|
| 1 | +import * as fs from 'node:fs'; |
| 2 | +import * as os from 'node:os'; |
| 3 | +import * as path from 'node:path'; |
1 | 4 | import * as git from '../../src/help/git'; |
2 | 5 | import * as shell from '../../src/help/shell'; |
3 | 6 |
|
4 | | -// mock shell.run |
5 | | -jest.mock('../../src/help/shell'); |
6 | | -const mockedShellRun = (shell.run as unknown) as jest.Mock<typeof shell.run>; |
7 | | - |
8 | | -// restore env after each test |
9 | | -const OLD_ENV = process.env; |
10 | | - |
| 7 | +let shellRunSpy: jest.SpyInstance<string, Parameters<typeof shell.run>>; |
11 | 8 | beforeEach(() => { |
12 | | - jest.resetModules(); // Most important - it clears the cache |
13 | | - process.env = { ...OLD_ENV }; // Make a copy |
| 9 | + shellRunSpy = jest.spyOn(shell, 'run'); |
14 | 10 | }); |
15 | | - |
16 | | -afterAll(() => { |
17 | | - process.env = OLD_ENV; // Restore old environment |
| 11 | +afterEach(() => { |
| 12 | + jest.clearAllMocks(); |
18 | 13 | }); |
19 | 14 |
|
20 | | -// test |
21 | | -test('clone with token', () => { |
22 | | - process.env.GITHUB_TOKEN = 'my-token'; |
23 | 15 |
|
24 | | - git.clone('github.com/cdklabs/publib', 'target'); |
25 | | - |
26 | | - expect(mockedShellRun.mock.calls).toHaveLength(1); |
27 | | - expect(mockedShellRun.mock.calls[0]).toEqual(['git clone https://[email protected]/cdklabs/publib.git target']); |
| 16 | +test('checkout with createIfMissing', () => { |
| 17 | + withTmpDir(() => { |
| 18 | + git.init(); |
| 19 | + git.checkout('main', { createIfMissing: true }); |
| 20 | + }); |
| 21 | + expect(shellRunSpy.mock.calls).toHaveLength(3); // init, show-branch, checkout -B |
| 22 | + expect(shellRunSpy.mock.calls[2]).toEqual(['git checkout -B main']); |
28 | 23 | }); |
29 | 24 |
|
30 | | -test('clone with ssh', () => { |
31 | | - process.env.GITHUB_USE_SSH = '1'; |
32 | | - |
33 | | - git.clone('github.com/cdklabs/publib', 'target'); |
34 | | - |
35 | | - expect(mockedShellRun.mock.calls).toHaveLength(1); |
36 | | - expect(mockedShellRun.mock.calls[0]).toEqual(['git clone [email protected]:cdklabs/publib.git target']); |
37 | | -}); |
38 | | - |
39 | | -test('throw exception without token or ssh', () => { |
40 | | - const t = () => git.clone('github.com/cdklabs/publib', 'target'); |
41 | | - expect(t).toThrow('GITHUB_TOKEN env variable is required when GITHUB_USE_SSH env variable is not used'); |
42 | | -}); |
43 | | - |
44 | | -test('throw exception without ghe authentication for github enterprise repo', () => { |
45 | | - const t = () => git.clone('github.corporate-enterprise.com/cdklabs/publib', 'target'); |
46 | | - expect(t).toThrow('GITHUB_TOKEN env variable is required when GITHUB_USE_SSH env variable is not used'); |
47 | | -}); |
48 | | - |
49 | | -test('throw exception with incomplete ghe authentication for github enterprise repo', () => { |
50 | | - process.env.GITHUB_ENTERPRISE_TOKEN = 'valid-token'; |
51 | | - const t = () => git.clone('github.corporate-enterprise.com/cdklabs/publib', 'target'); |
52 | | - expect(t).toThrow('GITHUB_TOKEN env variable is required when GITHUB_USE_SSH env variable is not used'); |
53 | | -}); |
54 | | - |
55 | | -test('clone with provided ghe authentication for github enterprise repo but no set github api url', () => { |
56 | | - process.env.GH_ENTERPRISE_TOKEN = 'valid-token'; |
57 | | - process.env.GH_HOST = 'github.corporate-enterprise.com'; |
58 | | - git.clone('github.corporate-enterprise.com/cdklabs/publib', 'target'); |
59 | | - expect(mockedShellRun.mock.calls).toHaveLength(1); |
60 | | - expect(mockedShellRun.mock.calls[0]).toEqual(['git clone https://[email protected]/cdklabs/publib.git target']); |
61 | | -}); |
62 | | - |
63 | | -test('clone with provided ghe authentication for github enterprise repo and with non-public github api url', () => { |
64 | | - process.env.GH_ENTERPRISE_TOKEN = 'valid-token'; |
65 | | - process.env.GH_HOST = 'github.corporate-enterprise.com'; |
66 | | - process.env.GITHUB_API_URL = 'https://api.github.corporate-enterprise.com'; |
67 | | - git.clone('github.corporate-enterprise.com/cdklabs/publib', 'target'); |
68 | | - expect(mockedShellRun.mock.calls).toHaveLength(1); |
69 | | - expect(mockedShellRun.mock.calls[0]).toEqual(['git clone https://[email protected]/cdklabs/publib.git target']); |
70 | | -}); |
| 25 | +function withTmpDir(fn: (tmpDir: string) => void) { |
| 26 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-')); |
| 27 | + const cwd = process.cwd(); |
| 28 | + try { |
| 29 | + process.chdir(tmpDir); |
| 30 | + fn(tmpDir); |
| 31 | + } finally { |
| 32 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 33 | + process.chdir(cwd); |
| 34 | + } |
| 35 | +} |
0 commit comments