|
16 | 16 | 'use strict';
|
17 | 17 |
|
18 | 18 | import * as assert from 'assert';
|
19 |
| -import {toWinDirFormat, getLocalAppDataPath} from '../src/utils'; |
| 19 | +import { toWin32Path, toWSLPath, getWSLLocalAppDataPath } from '../src/utils'; |
| 20 | +import * as sinon from 'sinon'; |
| 21 | +import * as child_process from 'child_process'; |
20 | 22 |
|
21 |
| -describe('WSL path format to Windows', () => { |
22 |
| - it('transforms basic path', () => { |
23 |
| - const wsl = '/mnt/c/Users/user1/AppData/'; |
24 |
| - const windows = 'C:\\Users\\user1\\AppData\\'; |
25 |
| - assert.strictEqual(toWinDirFormat(wsl), windows); |
26 |
| - }); |
| 23 | +const execFileSyncStub = sinon.stub(child_process, 'execFileSync').callThrough(); |
| 24 | + |
| 25 | +const asBuffer = (str: string): Buffer => Buffer.from(str, 'utf-8'); |
| 26 | + |
| 27 | +describe('toWin32Path', () => { |
| 28 | + beforeEach(() => execFileSyncStub.reset()); |
| 29 | + |
| 30 | + it('calls toWin32Path -w', () => { |
| 31 | + execFileSyncStub.returns(asBuffer('')); |
| 32 | + |
| 33 | + toWin32Path(''); |
| 34 | + |
| 35 | + assert.ok(execFileSyncStub.calledWith('wslpath', ['-w', ''])); |
| 36 | + }) |
| 37 | + |
| 38 | + describe('when the path is already in Windows format', () => { |
| 39 | + it('returns early', () => { |
| 40 | + execFileSyncStub.returns(asBuffer('')); |
27 | 41 |
|
28 |
| - it('transforms if drive letter is different than c', () => { |
29 |
| - const wsl = '/mnt/d/Users/user1/AppData'; |
30 |
| - const windows = 'D:\\Users\\user1\\AppData'; |
31 |
| - assert.strictEqual(toWinDirFormat(wsl), windows); |
| 42 | + assert.strictEqual(toWin32Path('D:\\'), 'D:\\'); |
| 43 | + assert.strictEqual(toWin32Path('C:\\'), 'C:\\'); |
| 44 | + |
| 45 | + assert.ok(execFileSyncStub.notCalled); |
| 46 | + }); |
| 47 | + }) |
| 48 | + |
| 49 | + describe('when wslpath is not available', () => { |
| 50 | + beforeEach(() => execFileSyncStub.throws(new Error('oh noes!'))); |
| 51 | + |
| 52 | + it('falls back to the toWinDirFormat method', () => { |
| 53 | + const wsl = '/mnt/c/Users/user1/AppData/'; |
| 54 | + const windows = 'C:\\Users\\user1\\AppData\\'; |
| 55 | + |
| 56 | + assert.strictEqual(toWin32Path(wsl), windows); |
| 57 | + }); |
| 58 | + |
| 59 | + it('supports the drive letter not being C', () => { |
| 60 | + const wsl = '/mnt/d/Users/user1/AppData'; |
| 61 | + const windows = 'D:\\Users\\user1\\AppData'; |
| 62 | + |
| 63 | + assert.strictEqual(toWin32Path(wsl), windows); |
| 64 | + }) |
32 | 65 | });
|
| 66 | +}) |
| 67 | + |
| 68 | +describe('toWSLPath', () => { |
| 69 | + beforeEach(() => execFileSyncStub.reset()); |
| 70 | + |
| 71 | + it('calls wslpath -u', () => { |
| 72 | + execFileSyncStub.returns(asBuffer('')); |
| 73 | + |
| 74 | + toWSLPath('', ''); |
| 75 | + |
| 76 | + assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', ''])); |
| 77 | + }) |
| 78 | + |
| 79 | + it('trims off the trailing newline', () => { |
| 80 | + execFileSyncStub.returns(asBuffer('the-path\n')); |
| 81 | + |
| 82 | + assert.strictEqual(toWSLPath('', ''), 'the-path'); |
| 83 | + }) |
| 84 | + |
| 85 | + describe('when wslpath is not available', () => { |
| 86 | + beforeEach(() => execFileSyncStub.throws(new Error('oh noes!'))); |
| 87 | + |
| 88 | + it('uses the fallback path', () => { |
| 89 | + assert.strictEqual( |
| 90 | + toWSLPath('C:/Program Files', '/mnt/c/Program Files'), |
| 91 | + '/mnt/c/Program Files' |
| 92 | + ); |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +describe('getWSLLocalAppDataPath', () => { |
| 98 | + beforeEach(() => execFileSyncStub.reset()); |
| 99 | + |
| 100 | + it('transforms it to a Linux path using wslpath', () => { |
| 101 | + execFileSyncStub.returns(asBuffer('/c/folder/')); |
33 | 102 |
|
34 |
| - it('getLocalAppDataPath returns a correct path', () => { |
35 | 103 | const path = '/mnt/c/Users/user1/.bin:/mnt/c/Users/user1:/mnt/c/Users/user1/AppData/';
|
36 |
| - const appDataPath = '/mnt/c/Users/user1/AppData/Local'; |
37 |
| - assert.strictEqual(getLocalAppDataPath(path), appDataPath); |
| 104 | + |
| 105 | + assert.strictEqual(getWSLLocalAppDataPath(path), '/c/folder/'); |
| 106 | + assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', 'c:\\Users\\user1\\AppData\\Local'])); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('when wslpath is not available', () => { |
| 110 | + beforeEach(() => execFileSyncStub.throws(new Error('oh noes!'))); |
| 111 | + |
| 112 | + it('falls back to the getLocalAppDataPath method', () => { |
| 113 | + const path = '/mnt/c/Users/user1/.bin:/mnt/c/Users/user1:/mnt/c/Users/user1/AppData/'; |
| 114 | + const appDataPath = '/mnt/c/Users/user1/AppData/Local'; |
| 115 | + |
| 116 | + assert.strictEqual(getWSLLocalAppDataPath(path), appDataPath); |
| 117 | + }); |
38 | 118 | });
|
39 | 119 | });
|
0 commit comments