From 74b39f5b6e8d2e247f95906b4649f121fcb45f53 Mon Sep 17 00:00:00 2001 From: Daniel Stockman <5605+evocateur@users.noreply.github.com> Date: Thu, 6 Jan 2022 14:04:15 -0800 Subject: [PATCH] test(integration): Respect global init.defaultBranch git config --- test/integration.test.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/test/integration.test.js b/test/integration.test.js index 1048f8148..d044a3a13 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -98,6 +98,9 @@ describe('lint-staged', () => { const globalConsoleTemp = console +// Tests should be resilient to `git config init.defaultBranch` that is _not_ "master" +let defaultBranchName = 'UNSET' + describe('lint-staged', () => { beforeAll(() => { console = makeConsoleMock() @@ -114,6 +117,10 @@ describe('lint-staged', () => { await appendFile('README.md', '# Test\n') await execGit(['add', 'README.md']) await execGit(['commit', '-m initial commit']) + + if (defaultBranchName === 'UNSET') { + defaultBranchName = await execGit(['rev-parse', '--abbrev-ref', 'HEAD']) + } }) afterEach(async () => { @@ -461,7 +468,7 @@ describe('lint-staged', () => { await gitCommit(fixJsConfig, ['-m commit a']) expect(await readFile('test.js')).toEqual(fileInBranchA) - await execGit(['checkout', 'master']) + await execGit(['checkout', defaultBranchName]) // Create another branch await execGit(['checkout', '-b', 'branch-b']) @@ -471,7 +478,7 @@ describe('lint-staged', () => { expect(await readFile('test.js')).toEqual(fileInBranchBFixed) // Merge first branch - await execGit(['checkout', 'master']) + await execGit(['checkout', defaultBranchName]) await execGit(['merge', 'branch-a']) expect(await readFile('test.js')).toEqual(fileInBranchA) expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('commit a') @@ -522,7 +529,7 @@ describe('lint-staged', () => { await gitCommit(fixJsConfig, ['-m commit a']) expect(await readFile('test.js')).toEqual(fileInBranchA) - await execGit(['checkout', 'master']) + await execGit(['checkout', defaultBranchName]) // Create another branch await execGit(['checkout', '-b', 'branch-b']) @@ -532,7 +539,7 @@ describe('lint-staged', () => { expect(await readFile('test.js')).toEqual(fileInBranchBFixed) // Merge first branch - await execGit(['checkout', 'master']) + await execGit(['checkout', defaultBranchName]) await execGit(['merge', 'branch-a']) expect(await readFile('test.js')).toEqual(fileInBranchA) expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('commit a') @@ -1083,7 +1090,7 @@ describe('lintStaged', () => { await execGit(['add', 'test.js'], { cwd }) await expect(execGit(['log', '-1'], { cwd })).rejects.toThrowErrorMatchingInlineSnapshot( - `"fatal: your current branch 'master' does not have any commits yet"` + `"fatal: your current branch '${defaultBranchName}' does not have any commits yet"` ) await gitCommit({