From 81d4f0680280fd20dc105c1daa575cb2017b84c7 Mon Sep 17 00:00:00 2001 From: joshaber Date: Thu, 10 Dec 2015 20:00:37 -0500 Subject: [PATCH] And that's why we write tests. --- spec/git-repository-async-spec.js | 29 ++++++++++++++++++++++++----- src/git-repository-async.js | 12 ++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/spec/git-repository-async-spec.js b/spec/git-repository-async-spec.js index 8850bd9a7..a9f1f9997 100644 --- a/spec/git-repository-async-spec.js +++ b/spec/git-repository-async-spec.js @@ -300,6 +300,25 @@ describe('GitRepositoryAsync', () => { }) }) + describe('.isProjectAtRoot()', () => { + it('returns true when the repository is at the root', async () => { + const workingDirectory = copyRepository() + atom.project.setPaths([workingDirectory]) + const repo = atom.project.getRepositories()[0].async + + const atRoot = await repo.isProjectAtRoot() + expect(atRoot).toBe(true) + }) + + it("returns false when the repository wasn't created with a project", async () => { + const workingDirectory = copyRepository() + const repo = GitRepositoryAsync.open(workingDirectory) + + const atRoot = await repo.isProjectAtRoot() + expect(atRoot).toBe(false) + }) + }) + describe('buffer events', () => { let repo @@ -310,8 +329,8 @@ describe('GitRepositoryAsync', () => { // When the path is added to the project, the repository is refreshed. We // need to wait for that to complete before the tests continue so that // we're in a known state. - repository = atom.project.getRepositories()[0].async - waitsFor(() => !repository._isRefreshing()) + repo = atom.project.getRepositories()[0].async + waitsFor(() => !repo._isRefreshing()) }) it('emits a status-changed event when a buffer is saved', async () => { @@ -320,7 +339,7 @@ describe('GitRepositoryAsync', () => { editor.insertNewline() const statusHandler = jasmine.createSpy('statusHandler') - repository.onDidChangeStatus(statusHandler) + repo.onDidChangeStatus(statusHandler) editor.save() waitsFor(() => statusHandler.callCount > 0) @@ -336,7 +355,7 @@ describe('GitRepositoryAsync', () => { fs.writeFileSync(editor.getPath(), 'changed') const statusHandler = jasmine.createSpy('statusHandler') - repository.onDidChangeStatus(statusHandler) + repo.onDidChangeStatus(statusHandler) editor.getBuffer().reload() waitsFor(() => statusHandler.callCount > 0) @@ -352,7 +371,7 @@ describe('GitRepositoryAsync', () => { fs.writeFileSync(editor.getPath(), 'changed') const statusHandler = jasmine.createSpy('statusHandler') - repository.onDidChangeStatus(statusHandler) + repo.onDidChangeStatus(statusHandler) editor.getBuffer().emitter.emit('did-change-path') waitsFor(() => statusHandler.callCount > 0) diff --git a/src/git-repository-async.js b/src/git-repository-async.js index a29b8f033..a87c93973 100644 --- a/src/git-repository-async.js +++ b/src/git-repository-async.js @@ -143,13 +143,13 @@ export default class GitRepositoryAsync { isProjectAtRoot () { if (!this.project) return Promise.resolve(false) - if (this.projectAtRoot) { - return this.projectAtRoot - } else { - this.projectAtRoot = Promise.resolve(() => { - return this.repoPromise.then(repo => this.project.relativize(repo.workdir())) - }) + if (!this.projectAtRoot) { + this.projectAtRoot = this.repoPromise + .then(repo => this.project.relativize(repo.workdir())) + .then(relativePath => relativePath === '') } + + return this.projectAtRoot } // Public: Makes a path relative to the repository's working directory.