Skip to content

Commit

Permalink
refactor: use extensionApi.env.is* instead of platform string compare (
Browse files Browse the repository at this point in the history
…#253)

Signed-off-by: Tony-Sol <tony.kent.nar.earth@gmail.com>
  • Loading branch information
tony-sol authored Nov 27, 2024
1 parent a033c6a commit 8e8265b
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 18 deletions.
3 changes: 0 additions & 3 deletions src/download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,6 @@ describe('install', () => {
});

test('user should be notified if system-wide installed failed', async () => {
Object.defineProperty(process, 'platform', {
value: 'linux',
});
(env.isLinux as boolean) = true;
const release: MinikubeGithubReleaseArtifactMetadata = {
tag: 'v1.2.3',
Expand Down
2 changes: 1 addition & 1 deletion src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class MinikubeDownload {
}

async makeExecutable(filePath: string): Promise<void> {
if (process.platform === 'darwin' || process.platform === 'linux') {
if (env.isLinux || env.isMac) {
// eslint-disable-next-line sonarjs/file-permissions
await promises.chmod(filePath, 0o755);
}
Expand Down
9 changes: 0 additions & 9 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ describe('getKubeConfig', () => {
describe('installBinaryToSystem', () => {
test('error: expect installBinaryToSystem to fail with a non existing binary', async () => {
// Mock the platform to be linux
Object.defineProperty(process, 'platform', {
value: 'linux',
});
vi.mocked(extensionApi.env).isLinux = true;

vi.spyOn(extensionApi.process, 'exec').mockImplementation(
Expand All @@ -200,9 +197,6 @@ describe('installBinaryToSystem', () => {

test('success: installBinaryToSystem on mac with /usr/local/bin already created', async () => {
// Mock the platform to be darwin
Object.defineProperty(process, 'platform', {
value: 'darwin',
});
vi.mocked(extensionApi.env).isMac = true;

// Mock existsSync to be true since within the function it's doing: !fs.existsSync(localBinDir)
Expand All @@ -223,9 +217,6 @@ describe('installBinaryToSystem', () => {

test('expect: installBinaryToSystem on linux with /usr/local/bin NOT created yet (expect mkdir -p command)', async () => {
// Mock the platform to be darwin
Object.defineProperty(process, 'platform', {
value: 'linux',
});
vi.mocked(extensionApi.env).isLinux = true;

// Mock existsSync to be false since within the function it's doing: !fs.existsSync(localBinDir)
Expand Down
8 changes: 3 additions & 5 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ export function getBinarySystemPath(binaryName: string): string {
// When running in a flatpak, we'll use flatpak-spawn to execute the command on the host
// @return the system-wide path where it is installed
export async function installBinaryToSystem(binaryPath: string, binaryName: string): Promise<string> {
const system = process.platform;

// Before copying the file, make sure it's executable (chmod +x) for Linux and Mac
if (system === 'linux' || system === 'darwin') {
if (extensionApi.env.isLinux || extensionApi.env.isMac) {
try {
await extensionApi.process.exec('chmod', ['+x', binaryPath]);
console.log(`Made ${binaryPath} executable`);
Expand All @@ -108,7 +106,7 @@ export async function installBinaryToSystem(binaryPath: string, binaryName: stri
const destinationPath: string = getBinarySystemPath(binaryName);
let command: string;
let args: string[];
if (system === 'win32') {
if (extensionApi.env.isWindows) {
command = 'copy';
args = [`"${binaryPath}"`, `"${destinationPath}"`];
} else {
Expand All @@ -119,7 +117,7 @@ export async function installBinaryToSystem(binaryPath: string, binaryName: stri
// If on macOS or Linux, check to see if the /usr/local/bin directory exists,
// if it does not, then add mkdir -p /usr/local/bin to the start of the command when moving the binary.
const localBinDir = '/usr/local/bin';
if ((system === 'linux' || system === 'darwin') && !fs.existsSync(localBinDir)) {
if ((extensionApi.env.isLinux || extensionApi.env.isMac) && !fs.existsSync(localBinDir)) {
await extensionApi.process.exec('mkdir', ['-p', localBinDir], { isAdmin: true });
}

Expand Down

0 comments on commit 8e8265b

Please sign in to comment.