Check whether existsSync() is available in GitRepositoryProvider before trying to call it.

This commit is contained in:
Michael Bolin
2015-02-23 09:30:43 -08:00
parent eb06cb7f97
commit 07039ba47a
2 changed files with 16 additions and 21 deletions

View File

@@ -55,14 +55,15 @@ describe "GitRepositoryProvider", ->
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", ->
describe "when specified a Directory without existsSync()", ->
it "returns null", ->
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: ->
# Tolerate an implementation of Directory that does not implement existsSync().
subdirectory = {}
directory =
getSubdirectory: ->
isRoot: -> true
spyOn(directory, "getSubdirectory").andReturn(subdirectory)
provider = new GitRepositoryProvider atom.project
@@ -70,13 +71,14 @@ describe "GitRepositoryProvider", ->
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", ->
it "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: ->
subdirectory = {}
directory =
getSubdirectory: ->
isRoot: -> true
spyOn(directory, "getSubdirectory").andReturn(subdirectory)
waitsForPromise ->

View File

@@ -11,7 +11,7 @@ findGitDirectorySync = (directory) ->
# can return cached values rather than always returning new objects:
# getParent(), getFile(), getSubdirectory().
gitDir = directory.getSubdirectory('.git')
if gitDir.existsSync() and isValidGitDirectorySync gitDir
if gitDir.existsSync?() and isValidGitDirectorySync gitDir
gitDir
else if directory.isRoot()
return null
@@ -26,9 +26,9 @@ isValidGitDirectorySync = (directory) ->
# To decide whether a directory has a valid .git folder, we use
# the heuristic adopted by the valid_repository_path() function defined in
# node_modules/git-utils/deps/libgit2/src/repository.c.
return directory.getSubdirectory('objects').existsSync() and
directory.getFile('HEAD').existsSync() and
directory.getSubdirectory('refs').existsSync()
return directory.getSubdirectory('objects').existsSync?() and
directory.getFile('HEAD').existsSync?() and
directory.getSubdirectory('refs').existsSync?()
# Provider that conforms to the atom.repository-provider@0.1.0 service.
module.exports =
@@ -51,13 +51,6 @@ 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
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.