-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: better workaround for git stash --keep-index bug
- Loading branch information
Showing
3 changed files
with
105 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import fs from 'fs-extra' | ||
import normalize from 'normalize-path' | ||
import os from 'os' | ||
import path from 'path' | ||
import nanoid from 'nanoid' | ||
|
||
import execGitBase from '../lib/execGit' | ||
import { cleanUntrackedFiles } from '../lib/gitWorkflow' | ||
|
||
jest.unmock('execa') | ||
|
||
jest.setTimeout(20000) | ||
|
||
const isAppveyor = !!process.env.APPVEYOR | ||
const osTmpDir = isAppveyor ? 'C:\\projects' : fs.realpathSync(os.tmpdir()) | ||
|
||
/** | ||
* Create temporary directory and return its path | ||
* @returns {Promise<String>} | ||
*/ | ||
const createTempDir = async () => { | ||
const dirname = path.resolve(osTmpDir, 'lint-staged-test', nanoid()) | ||
await fs.ensureDir(dirname) | ||
return dirname | ||
} | ||
|
||
/** | ||
* Remove temporary directory | ||
* @param {String} dirname | ||
* @returns {Promise<Void>} | ||
*/ | ||
const removeTempDir = async dirname => { | ||
await fs.remove(dirname) | ||
} | ||
|
||
let tmpDir, cwd | ||
|
||
/** Append to file, creating if it doesn't exist */ | ||
const appendFile = async (filename, content, dir = cwd) => | ||
fs.appendFile(path.resolve(dir, filename), content) | ||
|
||
/** Wrap execGit to always pass `gitOps` */ | ||
const execGit = async args => execGitBase(args, { cwd }) | ||
|
||
/** Initialize git repo for test */ | ||
const initGitRepo = async () => { | ||
await execGit('init') | ||
await execGit(['config', 'user.name', '"test"']) | ||
await execGit(['config', 'user.email', '"test@test.com"']) | ||
await appendFile('README.md', '# Test\n') | ||
await execGit(['add', 'README.md']) | ||
await execGit(['commit', '-m initial commit']) | ||
} | ||
|
||
describe('gitWorkflow', () => { | ||
beforeEach(async () => { | ||
tmpDir = await createTempDir() | ||
cwd = normalize(tmpDir) | ||
await initGitRepo() | ||
}) | ||
|
||
afterEach(async () => { | ||
if (!isAppveyor) { | ||
await removeTempDir(tmpDir) | ||
} | ||
}) | ||
|
||
describe('cleanUntrackedFiles', () => { | ||
it('should delete untracked, unstaged files', async () => { | ||
const testFile = path.resolve(cwd, 'test.js') | ||
await appendFile(testFile, 'test') | ||
expect(await fs.exists(testFile)).toEqual(true) | ||
await cleanUntrackedFiles(execGit) | ||
expect(await fs.exists(testFile)).toEqual(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters