Skip to content

Commit cdb023f

Browse files
Add sanitizations and validations
1 parent 4faf0d8 commit cdb023f

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

__tests__/unit/git-repo-dir.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import {GitDirNotExistsError} from '../../src/errors'
12
import {GitRepoDir} from '../../src/git-repo-dir'
3+
import {createInexistentTempDir} from '../../src/__tests__/helpers'
24

35
describe('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('../')

src/__tests__/helpers.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ import {GitRepo} from '../git-repo'
99
import {GitRepoDir} from '../git-repo-dir'
1010

1111
import {createTempDir} from 'jest-fixtures'
12+
import {join} from 'path'
1213
import {testConfiguration} from './config'
1314

1415
export 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+
1924
export async function createInitializedTempGnuPGHomeDir(): Promise<string> {
2025
const tempGnuPGHomeDir = await createTempDir()
2126
const keygrip = testConfiguration().gpg_signing_key.keygrip

src/errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
4047
export class PendingJobsLimitReachedError extends Error {
4148
constructor(committedMessage: CommittedMessage) {
4249
super(

src/git-repo-dir.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import {GitDirNotExistsError} from './errors'
2+
import {existsSync} from 'fs'
3+
14
export 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

src/git-repo.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)