Make sure that GitRepositoryProvider.repositoryForDirectorySync() returns null rather than throws.

The UI locks up if this method does not return.
This commit is contained in:
Michael Bolin
2015-02-20 17:53:14 -08:00
parent 25befa7368
commit 6d24aaf497
2 changed files with 38 additions and 0 deletions

View File

@@ -54,3 +54,33 @@ describe "GitRepositoryProvider", ->
directory = new Directory dirPath
provider.repositoryForDirectory(directory).then (result) ->
expect(result).toBe null
describe "when specified a Directory that throws an exception when explored", ->
it "catches the exception and returns null for the sync implementation", ->
provider = new GitRepositoryProvider atom.project
# Tolerate an implementation of Directory whose sync methods are unsupported.
subdirectory = existsSync: ->
spyOn(subdirectory, "existsSync").andThrow("sync method not supported")
directory = getSubdirectory: ->
spyOn(directory, "getSubdirectory").andReturn(subdirectory)
provider = new GitRepositoryProvider atom.project
repo = provider.repositoryForDirectorySync(directory)
expect(repo).toBe null
expect(directory.getSubdirectory).toHaveBeenCalledWith(".git")
it "catches the exception and returns a Promise that resolves to null for the async implementation", ->
provider = new GitRepositoryProvider atom.project
# Tolerate an implementation of Directory whose sync methods are unsupported.
subdirectory = existsSync: ->
spyOn(subdirectory, "existsSync").andThrow("sync method not supported")
directory = getSubdirectory: ->
spyOn(directory, "getSubdirectory").andReturn(subdirectory)
waitsForPromise ->
provider = new GitRepositoryProvider atom.project
provider.repositoryForDirectory(directory).then (repo) ->
expect(repo).toBe null
expect(directory.getSubdirectory).toHaveBeenCalledWith(".git")

View File

@@ -51,6 +51,14 @@ class GitRepositoryProvider
# * {GitRepository} if the given directory has a Git repository.
# * `null` if the given directory does not have a Git repository.
repositoryForDirectorySync: (directory) ->
# Ensure that this method does not throw.
try
@repositoryForDirectorySyncInternal(directory)
catch e
# TODO: Log error.
null
repositoryForDirectorySyncInternal: (directory) ->
# Only one GitRepository should be created for each .git folder. Therefore,
# we must check directory and its parent directories to find the nearest
# .git folder.