|
| 1 | +import { promiseError } from '@kwsites/promise-result'; |
| 2 | +import { assertGitError, closeWithSuccess, newSimpleGit } from '../__fixtures__'; |
| 3 | +import { mockChildProcessModule } from '../__mocks__/mock-child-process'; |
| 4 | + |
| 5 | +describe('binaryPlugin', () => { |
| 6 | + it.each<[string, undefined | string | [string] | [string, string], string[]]>([ |
| 7 | + ['undefined', undefined, ['git']], |
| 8 | + ['string', 'simple', ['simple']], |
| 9 | + ['string array', ['array'], ['array']], |
| 10 | + ['strings array', ['array', 'tuple'], ['array', 'tuple']], |
| 11 | + ])('allows binary set to %s', async (_, binary, command) => { |
| 12 | + newSimpleGit({ binary }).raw('hello'); |
| 13 | + |
| 14 | + expect(await expected()).toEqual([...command, 'hello']); |
| 15 | + }); |
| 16 | + |
| 17 | + each( |
| 18 | + 'valid', |
| 19 | + './bin/git', |
| 20 | + 'c:\\path\\to\\git.exe', |
| 21 | + 'custom-git', |
| 22 | + 'GIT' |
| 23 | + )('allows valid syntax "%s"', async (binary) => { |
| 24 | + newSimpleGit({ binary }).raw('hello'); |
| 25 | + expect(await expected()).toEqual([binary, 'hello']); |
| 26 | + }); |
| 27 | + |
| 28 | + each( |
| 29 | + 'long:\\path\\git.exe', |
| 30 | + 'space fail', |
| 31 | + '"dquote fail"', |
| 32 | + "'squote fail'", |
| 33 | + '$', |
| 34 | + '!' |
| 35 | + )('rejects invalid syntax "%s"', async (binary) => { |
| 36 | + assertGitError( |
| 37 | + await promiseError((async () => newSimpleGit({ binary }).raw('hello'))()), |
| 38 | + 'Invalid value supplied for custom binary' |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('works with config plugin', async () => { |
| 43 | + newSimpleGit({ binary: ['alpha', 'beta'], config: ['gamma'] }).raw('hello'); |
| 44 | + expect(await expected()).toEqual(['alpha', 'beta', '-c', 'gamma', 'hello']); |
| 45 | + }); |
| 46 | + |
| 47 | + it('allows reconfiguring binary', async () => { |
| 48 | + const git = newSimpleGit().raw('a'); |
| 49 | + expect(await expected()).toEqual(['git', 'a']); |
| 50 | + |
| 51 | + git.customBinary('next').raw('b'); |
| 52 | + expect(await expected()).toEqual(['next', 'b']); |
| 53 | + |
| 54 | + git.customBinary(['abc', 'def']).raw('g'); |
| 55 | + expect(await expected()).toEqual(['abc', 'def', 'g']); |
| 56 | + }); |
| 57 | + |
| 58 | + it('rejects reconfiguring to an invalid binary', async () => { |
| 59 | + const git = newSimpleGit().raw('a'); |
| 60 | + expect(await expected()).toEqual(['git', 'a']); |
| 61 | + |
| 62 | + assertGitError( |
| 63 | + await promiseError((async () => git.customBinary('not valid'))()), |
| 64 | + 'Invalid value supplied for custom binary' |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('allows configuring to bad values when overridden', async () => { |
| 69 | + const git = newSimpleGit({ unsafe: { allowUnsafeCustomBinary: true }, binary: '$' }).raw('a'); |
| 70 | + expect(await expected()).toEqual(['$', 'a']); |
| 71 | + |
| 72 | + git.customBinary('!').raw('b'); |
| 73 | + expect(await expected()).toEqual(['!', 'b']); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +function each(...things: string[]) { |
| 78 | + return it.each(things.map((thing) => [thing])); |
| 79 | +} |
| 80 | + |
| 81 | +async function expected() { |
| 82 | + await closeWithSuccess(); |
| 83 | + const recent = mockChildProcessModule.$mostRecent(); |
| 84 | + return [recent.$command, ...recent.$args]; |
| 85 | +} |
0 commit comments