Skip to content

Commit bd292c2

Browse files
committed
re-write unit tests
1 parent ff0cf37 commit bd292c2

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

packages/core/src/test/shared/utilities/pathFind.test.ts

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import * as os from 'os'
99
import * as path from 'path'
1010
import * as testutil from '../../testUtil'
1111
import { fs } from '../../../shared'
12-
import { findSshPath, findTypescriptCompiler, getVscodeCliPath } from '../../../shared/utilities/pathFind'
13-
import { isWin } from '../../../shared/vscode/env'
12+
import { findSshPath, findTypescriptCompiler, getVscodeCliPath, tryRun } from '../../../shared/utilities/pathFind'
13+
import { isCI, isWin } from '../../../shared/vscode/env'
1414

1515
describe('pathFind', function () {
1616
it('findTypescriptCompiler()', async function () {
@@ -53,7 +53,11 @@ describe('pathFind', function () {
5353
testutil.setEnv(previousEnv)
5454
})
5555

56-
it('first tries ssh in $PATH', async function () {
56+
it('first tries ssh in $PATH (Non-Windows)', async function () {
57+
// skip on windows because ssh in path will never work with .exe extension.
58+
if (isWin()) {
59+
return
60+
}
5761
const workspace = await testutil.createTestWorkspaceFolder()
5862
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
5963

@@ -68,30 +72,54 @@ describe('pathFind', function () {
6872
assert.strictEqual(secondResult, 'ssh')
6973
})
7074

71-
it('only returns executable ssh path', async function () {
72-
const workspace = await testutil.createTestWorkspaceFolder()
73-
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
74-
await fs.writeFile(fakeSshPath, 'this is not executable')
75-
76-
testutil.setEnv(testutil.envWithNewPath(workspace.uri.fsPath))
77-
const firstResult = await findSshPath(false)
78-
assert.notStrictEqual(firstResult, 'ssh')
75+
it('only returns valid executable ssh path (CI + Local Non-Windows)', async function () {
76+
// In CI, we can assume Windows machines will have ssh.
77+
if (!isCI()) {
78+
return
79+
}
80+
// On local non-windows, we can overwrite path and create our own executable to find.
81+
if (!isWin()) {
82+
const workspace = await testutil.createTestWorkspaceFolder()
83+
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
84+
85+
testutil.setEnv(testutil.envWithNewPath(workspace.uri.fsPath))
86+
87+
await testutil.createExecutableFile(fakeSshPath, '')
88+
}
89+
90+
const ssh = await findSshPath(false)
91+
assert.ok(ssh)
92+
const result = await tryRun(ssh, [])
93+
assert.ok(result)
7994
})
8095

81-
it('caches result from previous runs', async function () {
96+
it('caches result from previous runs (CI + Local Non-Windows)', async function () {
97+
// In CI, we can assume Windows machines will have ssh.
98+
if (!isCI()) {
99+
return
100+
}
101+
// On local non-windows, we can overwrite path and create our own executable to find.
82102
const workspace = await testutil.createTestWorkspaceFolder()
83-
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
84-
await testutil.createExecutableFile(fakeSshPath, '')
103+
// We move the ssh to a temp directory temporarily to test if cache works.
104+
const tempLocation = path.join(workspace.uri.fsPath, 'temp-ssh')
85105

86-
testutil.setEnv(testutil.envWithNewPath(workspace.uri.fsPath))
87-
const firstResult = await findSshPath(true)
106+
if (!isWin()) {
107+
const fakeSshPath = path.join(workspace.uri.fsPath, `ssh`)
108+
109+
testutil.setEnv(testutil.envWithNewPath(workspace.uri.fsPath))
110+
111+
await testutil.createExecutableFile(fakeSshPath, '')
112+
}
113+
114+
const ssh1 = await findSshPath(true)
115+
116+
await fs.rename(ssh1!, tempLocation)
88117

89-
await fs.delete(fakeSshPath)
118+
const ssh2 = await findSshPath(true)
90119

91-
const secondResult = await findSshPath(true)
120+
assert.strictEqual(ssh1, ssh2)
92121

93-
assert.strictEqual(firstResult, secondResult)
94-
assert.ok(secondResult)
122+
await fs.rename(tempLocation, ssh1!)
95123
})
96124
})
97125
})

packages/core/src/test/shared/utilities/testUtils.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import assert from 'assert'
66
import { createExecutableFile, createTestWorkspaceFolder, envWithNewPath, readEnv, setEnv } from '../../testUtil'
77
import path from 'path'
88
import { fs } from '../../../shared'
9+
import { isWin } from '../../../shared/vscode/env'
910
import { tryRun } from '../../../shared/utilities/pathFind'
1011

1112
describe('envWithNewPath', function () {
@@ -30,7 +31,7 @@ describe('writeEnv', function () {
3031
describe('createExecutableFile', function () {
3132
it('creates a file that can be executed', async function () {
3233
const tempDir = await createTestWorkspaceFolder()
33-
const filePath = path.join(tempDir.uri.fsPath, `exec`)
34+
const filePath = path.join(tempDir.uri.fsPath, `exec${isWin() ? '.cmd' : ''}`)
3435
await createExecutableFile(filePath, '')
3536

3637
const result = await tryRun(filePath, [], 'yes')

0 commit comments

Comments
 (0)