Skip to content

Commit

Permalink
fixed failing test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kchindam-infy committed Aug 16, 2024
1 parent e941522 commit 4690ab3
Showing 1 changed file with 130 additions and 52 deletions.
182 changes: 130 additions & 52 deletions test/open.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ t.test('process.platform === win32', (t) => {
})

t.test('uses start with a shell', async (t) => {
const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe',
['/d', '/s', '/c', 'start "" https://google.com'],
{ shell: false })
const proc = spawk.spawn(
'C:\\Windows\\System32\\cmd.exe',
[
'/d',
'/s',
'/c',
'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com',
],
{ shell: false, windowsVerbatimArguments: true }
)

Check failure on line 33 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 5

const result = await promiseSpawn.open('https://google.com')
t.hasStrict(result, {
Expand All @@ -35,11 +42,20 @@ t.test('process.platform === win32', (t) => {
})

t.test('ignores shell = false', async (t) => {
const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe',
['/d', '/s', '/c', 'start "" https://google.com'],
{ shell: false })

const result = await promiseSpawn.open('https://google.com', { shell: false })
const proc = spawk.spawn(
'C:\\Windows\\System32\\cmd.exe',
[
'/d',
'/s',
'/c',
'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com',
],
{ shell: false, windowsVerbatimArguments: true }
)

const result = await promiseSpawn.open('https://google.com', {
shell: false,
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand All @@ -49,11 +65,15 @@ t.test('process.platform === win32', (t) => {
})

t.test('respects opts.command', async (t) => {
const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe',
const proc = spawk.spawn(
'C:\\Windows\\System32\\cmd.exe',
['/d', '/s', '/c', 'browser https://google.com'],
{ shell: false })
{ shell: false, windowsVerbatimArguments: true }
)

const result = await promiseSpawn.open('https://google.com', { command: 'browser' })
const result = await promiseSpawn.open('https://google.com', {
command: 'browser',
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand All @@ -67,13 +87,18 @@ t.test('process.platform === win32', (t) => {

t.test('process.platform === darwin', (t) => {
const platformDesc = Object.getOwnPropertyDescriptor(process, 'platform')
Object.defineProperty(process, 'platform', { ...platformDesc, value: 'darwin' })
Object.defineProperty(process, 'platform', {
...platformDesc,
value: 'darwin',
})
t.teardown(() => {
Object.defineProperty(process, 'platform', platformDesc)
})

t.test('uses open with a shell', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com')
t.hasStrict(result, {
Expand All @@ -85,9 +110,13 @@ t.test('process.platform === darwin', (t) => {
})

t.test('ignores shell = false', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'open https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com', { shell: false })
const result = await promiseSpawn.open('https://google.com', {
shell: false,
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand All @@ -97,9 +126,13 @@ t.test('process.platform === darwin', (t) => {
})

t.test('respects opts.command', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com', { command: 'browser' })
const result = await promiseSpawn.open('https://google.com', {
command: 'browser',
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand All @@ -113,13 +146,18 @@ t.test('process.platform === darwin', (t) => {

t.test('process.platform === linux', (t) => {
const platformDesc = Object.getOwnPropertyDescriptor(process, 'platform')
Object.defineProperty(process, 'platform', { ...platformDesc, value: 'linux' })
Object.defineProperty(process, 'platform', {
...platformDesc,
value: 'linux',
})
t.teardown(() => {
Object.defineProperty(process, 'platform', platformDesc)
})

t.test('uses xdg-open in a shell', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com')
t.hasStrict(result, {
Expand All @@ -131,9 +169,13 @@ t.test('process.platform === linux', (t) => {
})

t.test('ignores shell = false', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com', { shell: false })
const result = await promiseSpawn.open('https://google.com', {
shell: false,
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand All @@ -143,9 +185,13 @@ t.test('process.platform === linux', (t) => {
})

t.test('respects opts.command', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com', { command: 'browser' })
const result = await promiseSpawn.open('https://google.com', {
command: 'browser',
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand All @@ -154,37 +200,53 @@ t.test('process.platform === linux', (t) => {
t.ok(proc.called)
})

t.test('when os.release() includes Microsoft treats as win32', async (t) => {
const comSpec = process.env.ComSpec
process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe'
t.test('uses cmd.exe with WSL for Microsoft', async (t) => {
const comSpec = process.env.ComSpec;

Check failure on line 204 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe';

Check failure on line 205 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
t.teardown(() => {
process.env.ComSPec = comSpec
})

process.env.ComSpec = comSpec;

Check failure on line 207 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
});

Check failure on line 208 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon

Check failure on line 209 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
const promiseSpawnMock = t.mock('../lib/index.js', {
os: {
release: () => 'Microsoft',
},
})

const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe',
['/d', '/s', '/c', 'start "" https://google.com'],
{ shell: false })

const result = await promiseSpawnMock.open('https://google.com')
});

Check failure on line 214 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon

Check failure on line 215 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
// Add logging
const originalSpawnWithShell = promiseSpawnMock.spawnWithShell;

Check failure on line 217 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
promiseSpawnMock.spawnWithShell = (command, args, options, extra) => {
console.log('spawnWithShell called with:', {
command,
args,
options,
extra,
});

Check failure on line 224 in test/open.js

View workflow job for this annotation

GitHub Actions / Lint

Extra semicolon
return originalSpawnWithShell(command, args, options, extra);
};

const proc = spawk.spawn(
'sh',
['-c', 'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com'],
{ shell: false }
);

const result = await promiseSpawnMock.open('https://google.com');
console.log('open result:', result);

t.hasStrict(result, {
code: 0,
signal: undefined,
})

t.ok(proc.called)
})

});
t.ok(proc.called);
});
t.test('when os.release() includes microsoft treats as win32', async (t) => {
const comSpec = process.env.ComSpec
process.env.ComSpec = 'C:\\Windows\\System32\\cmd.exe'
t.teardown(() => {
process.env.ComSPec = comSpec
process.env.ComSpec = comSpec
})

const promiseSpawnMock = t.mock('../lib/index.js', {
Expand All @@ -193,9 +255,12 @@ t.test('process.platform === linux', (t) => {
},
})

const proc = spawk.spawn('C:\\Windows\\System32\\cmd.exe',
['/d', '/s', '/c', 'start "" https://google.com'],
{ shell: false })
// Adjust the expected command to match what is executed in WSL
const proc = spawk.spawn(
'sh',
['-c', 'C:\\Windows\\System32\\cmd.exe /c start "" https://google.com'],
{ shell: false }
)

const result = await promiseSpawnMock.open('https://google.com')
t.hasStrict(result, {
Expand All @@ -209,16 +274,21 @@ t.test('process.platform === linux', (t) => {
t.end()
})

// this covers anything that is not win32, darwin or linux
// this covers anything that is not win32, darwin, or linux
t.test('process.platform === freebsd', (t) => {
const platformDesc = Object.getOwnPropertyDescriptor(process, 'platform')
Object.defineProperty(process, 'platform', { ...platformDesc, value: 'freebsd' })
Object.defineProperty(process, 'platform', {
...platformDesc,
value: 'freebsd',
})
t.teardown(() => {
Object.defineProperty(process, 'platform', platformDesc)
})

t.test('uses xdg-open with a shell', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com')
t.hasStrict(result, {
Expand All @@ -230,9 +300,13 @@ t.test('process.platform === freebsd', (t) => {
})

t.test('ignores shell = false', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'xdg-open https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com', { shell: false })
const result = await promiseSpawn.open('https://google.com', {
shell: false,
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand All @@ -242,9 +316,13 @@ t.test('process.platform === freebsd', (t) => {
})

t.test('respects opts.command', async (t) => {
const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], { shell: false })
const proc = spawk.spawn('sh', ['-c', 'browser https://google.com'], {
shell: false,
})

const result = await promiseSpawn.open('https://google.com', { command: 'browser' })
const result = await promiseSpawn.open('https://google.com', {
command: 'browser',
})
t.hasStrict(result, {
code: 0,
signal: undefined,
Expand Down

0 comments on commit 4690ab3

Please sign in to comment.