From 16b8c293a19f2d39166ecfd6e9bd76070a600754 Mon Sep 17 00:00:00 2001 From: Lukas Geiger Date: Sun, 14 Jan 2018 15:20:58 +0100 Subject: [PATCH] :white_check_mark: Add tests for repositoryForDirectorySync Since `repositoryForDirectory` and `repositoryForDirectorySync` don't share the same implementation anymore. --- spec/git-repository-provider-spec.js | 88 ++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 6 deletions(-) diff --git a/spec/git-repository-provider-spec.js b/spec/git-repository-provider-spec.js index 24993fe9b..2e679e755 100644 --- a/spec/git-repository-provider-spec.js +++ b/spec/git-repository-provider-spec.js @@ -82,6 +82,88 @@ describe('GitRepositoryProvider', () => { }) }) + describe('when specified a Directory without exists()', () => { + let directory + + beforeEach(() => { + // An implementation of Directory that does not implement existsSync(). + const subdirectory = {} + directory = { + getSubdirectory () {}, + isRoot () { return true } + } + spyOn(directory, 'getSubdirectory').andReturn(subdirectory) + }) + + it('returns a Promise that resolves to null', async () => { + const repo = await provider.repositoryForDirectory(directory) + expect(repo).toBe(null) + expect(directory.getSubdirectory).toHaveBeenCalledWith('.git') + }) + }) + }) + + describe('.repositoryForDirectorySync(directory)', () => { + describe('when specified a Directory with a Git repository', () => { + it('resolves with a GitRepository', async () => { + const directory = new Directory(path.join(__dirname, 'fixtures', 'git', 'master.git')) + const result = provider.repositoryForDirectorySync(directory) + expect(result).toBeInstanceOf(GitRepository) + expect(provider.pathToRepository[result.getPath()]).toBeTruthy() + expect(result.getType()).toBe('git') + + // Refresh should be started + await new Promise(resolve => result.onDidChangeStatuses(resolve)) + }) + + it('resolves with the same GitRepository for different Directory objects in the same repo', () => { + const firstRepo = provider.repositoryForDirectorySync( + new Directory(path.join(__dirname, 'fixtures', 'git', 'master.git')) + ) + const secondRepo = provider.repositoryForDirectorySync( + new Directory(path.join(__dirname, 'fixtures', 'git', 'master.git', 'objects')) + ) + + expect(firstRepo).toBeInstanceOf(GitRepository) + expect(firstRepo).toBe(secondRepo) + }) + }) + + describe('when specified a Directory without a Git repository', () => { + it('resolves with null', () => { + const directory = new Directory(temp.mkdirSync('dir')) + const repo = provider.repositoryForDirectorySync(directory) + expect(repo).toBe(null) + }) + }) + + describe('when specified a Directory with an invalid Git repository', () => { + it('resolves with null', () => { + const dirPath = temp.mkdirSync('dir') + fs.writeFileSync(path.join(dirPath, '.git', 'objects'), '') + fs.writeFileSync(path.join(dirPath, '.git', 'HEAD'), '') + fs.writeFileSync(path.join(dirPath, '.git', 'refs'), '') + + const directory = new Directory(dirPath) + const repo = provider.repositoryForDirectorySync(directory) + expect(repo).toBe(null) + }) + }) + + describe('when specified a Directory with a valid gitfile-linked repository', () => { + it('returns a Promise that resolves to a GitRepository', () => { + const gitDirPath = path.join(__dirname, 'fixtures', 'git', 'master.git') + const workDirPath = temp.mkdirSync('git-workdir') + fs.writeFileSync(path.join(workDirPath, '.git'), `gitdir: ${gitDirPath}\n`) + + const directory = new Directory(workDirPath) + const result = provider.repositoryForDirectorySync(directory) + expect(result).toBeInstanceOf(GitRepository) + expect(provider.pathToRepository[result.getPath()]).toBeTruthy() + expect(result.getType()).toBe('git') + }) + }) + describe('when specified a Directory without existsSync()', () => { let directory @@ -100,12 +182,6 @@ describe('GitRepositoryProvider', () => { expect(repo).toBe(null) expect(directory.getSubdirectory).toHaveBeenCalledWith('.git') }) - - it('returns a Promise that resolves to null for the async implementation', async () => { - const repo = await provider.repositoryForDirectory(directory) - expect(repo).toBe(null) - expect(directory.getSubdirectory).toHaveBeenCalledWith('.git') - }) }) }) })