File tree Expand file tree Collapse file tree 5 files changed +34
-7
lines changed Expand file tree Collapse file tree 5 files changed +34
-7
lines changed Original file line number Diff line number Diff line change 1+ import { GitDirNotExistsError } from '../../src/errors'
12import { GitRepoDir } from '../../src/git-repo-dir'
3+ import { createInexistentTempDir } from '../../src/__tests__/helpers'
24
35describe ( 'GitRepoDir' , ( ) => {
46 it ( 'should contain dir path for the Git repo' , ( ) => {
@@ -7,6 +9,13 @@ describe('GitRepoDir', () => {
79 expect ( gitRepoDir . getDirPath ( ) ) . toBe ( './' )
810 } )
911
12+ it ( 'should throw on a non-existent Dir initialization' , async ( ) => {
13+ const inexistentDir = await createInexistentTempDir ( )
14+ const failingRepoDirTest = ( ) : GitRepoDir => new GitRepoDir ( inexistentDir )
15+
16+ expect ( failingRepoDirTest ) . toThrow ( GitDirNotExistsError )
17+ } )
18+
1019 it ( 'should compare two git repo dirs' , ( ) => {
1120 const gitRepoDir1 = new GitRepoDir ( './' )
1221 const gitRepoDir2 = new GitRepoDir ( '../' )
Original file line number Diff line number Diff line change @@ -9,13 +9,18 @@ import {GitRepo} from '../git-repo'
99import { GitRepoDir } from '../git-repo-dir'
1010
1111import { createTempDir } from 'jest-fixtures'
12+ import { join } from 'path'
1213import { testConfiguration } from './config'
1314
1415export async function createTempEmptyDir ( ) : Promise < string > {
1516 const tempGitDirPath = await createTempDir ( )
1617 return tempGitDirPath
1718}
1819
20+ export async function createInexistentTempDir ( ) : Promise < string > {
21+ return join ( await createTempEmptyDir ( ) , `inexistent` )
22+ }
23+
1924export async function createInitializedTempGnuPGHomeDir ( ) : Promise < string > {
2025 const tempGnuPGHomeDir = await createTempDir ( )
2126 const keygrip = testConfiguration ( ) . gpg_signing_key . keygrip
Original file line number Diff line number Diff line change @@ -37,6 +37,13 @@ export class GitDirNotInitializedError extends Error {
3737 }
3838}
3939
40+ export class GitDirNotExistsError extends Error {
41+ constructor ( dir : string ) {
42+ super ( `Git dir: ${ dir } does not exist or is not reachable` )
43+ Object . setPrototypeOf ( this , GitDirNotExistsError . prototype )
44+ }
45+ }
46+
4047export class PendingJobsLimitReachedError extends Error {
4148 constructor ( committedMessage : CommittedMessage ) {
4249 super (
Original file line number Diff line number Diff line change 1+ import { GitDirNotExistsError } from './errors'
2+ import { existsSync } from 'fs'
3+
14export class GitRepoDir {
25 private readonly dirPath : string
36
47 constructor ( dirPath : string ) {
5- // TODO: validate dir path
8+ if ( ! existsSync ( dirPath ) ) {
9+ throw new GitDirNotExistsError ( dirPath )
10+ }
611 this . dirPath = dirPath
712 }
813
Original file line number Diff line number Diff line change @@ -17,8 +17,9 @@ export class GitRepo {
1717
1818 isInitialized ( ) : boolean {
1919 try {
20- if ( ! this . dirPathExists ( ) ) {
21- return false
20+ // Make sure the string we will pass to to the shell is an actual dir
21+ if ( ! existsSync ( this . dir . getDirPath ( ) ) ) {
22+ throw new Error ( )
2223 }
2324 execSync ( `cd ${ this . getDirPath ( ) } && git status` )
2425 } catch {
@@ -35,10 +36,6 @@ export class GitRepo {
3536 return this . dir . getDirPath ( )
3637 }
3738
38- dirPathExists ( ) : boolean {
39- return existsSync ( this . getDirPath ( ) )
40- }
41-
4239 async init ( ) : Promise < void > {
4340 await this . git . init ( )
4441 }
@@ -61,6 +58,10 @@ export class GitRepo {
6158 throw new GitDirNotInitializedError ( this . dir . getDirPath ( ) )
6259 }
6360 try {
61+ // Make sure the string we will pass to to the shell is an actual dir
62+ if ( ! existsSync ( this . dir . getDirPath ( ) ) ) {
63+ throw new Error ( )
64+ }
6465 execSync ( `cd ${ this . dir . getDirPath ( ) } && git log -n 0` )
6566 } catch ( err ) {
6667 // No commits yet
You can’t perform that action at this time.
0 commit comments