|
| 1 | +import assert from 'assert'; |
| 2 | +import path from 'path'; |
| 3 | +import { isGitDir, resolveGitDir } from '@discoveryjs/scan-git'; |
| 4 | + |
| 5 | +const cwd = process.cwd(); |
| 6 | +const gitdir = path.join(cwd, '.git'); |
| 7 | + |
| 8 | +describe('Git Directory Utilities', () => { |
| 9 | + describe('isGitDir()', () => { |
| 10 | + it('should return false for non-Git directory', async () => { |
| 11 | + const actual = await isGitDir(cwd); |
| 12 | + assert.strictEqual(actual, false); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return true for valid Git directory', async () => { |
| 16 | + const actual = await isGitDir(gitdir); |
| 17 | + assert.strictEqual(actual, true); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('resolveGitDir()', () => { |
| 22 | + describe('absolute paths', () => { |
| 23 | + it('should resolve current working directory to Git directory', async () => { |
| 24 | + const actual = await resolveGitDir(cwd); |
| 25 | + assert.strictEqual(actual, gitdir); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should resolve .git directory path to itself', async () => { |
| 29 | + const actual = await resolveGitDir(gitdir); |
| 30 | + assert.strictEqual(actual, gitdir); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('relative paths', () => { |
| 35 | + it('should resolve empty path to Git directory', async () => { |
| 36 | + const actual = await resolveGitDir(''); |
| 37 | + assert.strictEqual(actual, gitdir); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should resolve .git relative path to Git directory', async () => { |
| 41 | + const actual = await resolveGitDir('.git'); |
| 42 | + assert.strictEqual(actual, gitdir); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should throws for non existing paths', () => { |
| 47 | + const testdir = path.resolve(cwd, 'non-exists'); |
| 48 | + |
| 49 | + return assert.rejects( |
| 50 | + () => resolveGitDir(testdir), |
| 51 | + (e: Error) => e.message.endsWith(`No such directory "${testdir}"`) |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should throws for non directory paths', () => { |
| 56 | + const testpath = path.resolve(cwd, 'package.json'); |
| 57 | + |
| 58 | + return assert.rejects( |
| 59 | + () => resolveGitDir(testpath), |
| 60 | + (e: Error) => e.message.endsWith(`Not a directory "${testpath}"`) |
| 61 | + ); |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments