Skip to content

Commit

Permalink
Fixing issue #19 with unit-tests (#25)
Browse files Browse the repository at this point in the history
* Adding some simple constructor unit-tests as a POC

* Windows version of (simplified) bash-script

* Added (failing) test for ignore status. Fixed a small issue with before/after hook-scoping

* Status test-script for windows

* adding js & js.map files to the ignore list in order to improve debugger-support

* Fixed hiding hg-ignored files
- Atom checks ignore-status with absolute paths, so included absolute paths in cache
- The refreshstatus promise was never resolved, so any subsequent actions where left hanging

* Needed to normalize the slashes for windows

* Travis test (#1)

* Travis trial

Trying to get travis to build only a branch

* Specifying node-versions

* App veyor trial (#2)

Added an AppVeyor yaml file.

For some reason the first test-fixture run is a lot slower than the others... So added a longer timeout as a workaround.
  • Loading branch information
TomKemperNL authored and victor-torres committed Sep 26, 2016
1 parent 6c4e0e5 commit da37ad7
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules/
.DS_Store
test/test_repo
*.js
*.js.map
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "6"
- "4"
21 changes: 21 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Test against this version of Node.js
environment:
nodejs_version: "6.2.0"

# Install scripts. (runs after repo cloning)
install:
# Get the latest stable version of Node.js or io.js
- ps: Install-Product node $env:nodejs_version
# install modules
- npm install

# Post-install test scripts.
test_script:
# Output useful info for debugging.
- node --version
- npm --version
# run tests
- npm test -- -t 10000

# Don't actually build.
build: off
7 changes: 4 additions & 3 deletions lib/hg-repository.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class HgRepository
constructor: (path, options={}) ->
@emitter = new Emitter
@subscriptions = new CompositeDisposable

@repo = HgUtils.open(path)

unless @repo?
throw new Error("No Mercurial repository found searching path: #{path.path}")
throw new Error("No Mercurial repository found searching path: #{path}")

# asyncOptions = _.clone(options)
@async = HgRepositoryAsync.open(path, options)
Expand Down Expand Up @@ -421,7 +421,7 @@ class HgRepository
# updates the relevant properties.
refreshStatus: ->
new Promise((resolve, reject) =>
@cachedIgnoreStatuses = @getRepo().getRecursiveIgnoreStatuses()
@cachedIgnoreStatuses = (@slashPath ignored for ignored in @getRepo().getRecursiveIgnoreStatuses())

statusesDidChange = false
if @getRepo().checkRepositoryHasChanged()
Expand All @@ -438,4 +438,5 @@ class HgRepository
statusesDidChange = true

if statusesDidChange then @emitter.emit 'did-change-statuses'
resolve()
)
11 changes: 7 additions & 4 deletions lib/hg-utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Repository
@binaryAvailable = false
return @binaryAvailable

exists: () ->
return fs.existsSync(@rootPath + '/.hg')

# Parses info from `hg info` and `hgversion` command and checks if repo infos have changed
# since last check
#
Expand Down Expand Up @@ -245,7 +248,7 @@ class Repository

return '' unless @isCommandForRepo(params)

child = spawnSync('hg', params)
child = spawnSync('hg', params, { cwd: @rootPath })
if child.status != 0
if child.stderr
throw new Error(child.stderr.toString())
Expand Down Expand Up @@ -293,7 +296,7 @@ class Repository
# Returns a {Array} with path and statusnumber
getRecursiveIgnoreStatuses: () ->
try
files = @hgCommand(['status', @rootPath])
files = @hgCommand(['status', @rootPath, "-A"])
catch error
@handleHgError(error)
return []
Expand All @@ -309,7 +312,7 @@ class Repository
if (status is 'I') # || status is '?')
items.push(pathPart.replace('..', ''))

return items
(path.join @rootPath, item for item in items)

# Returns on success an hg-status array. Otherwise null.
# Array keys are paths, values {Number} representing the status
Expand Down Expand Up @@ -453,7 +456,7 @@ exports.isStatusStaged = (status) ->
# Returns a new {Repository} object
openRepository = (repositoryPath) ->
repository = new Repository(repositoryPath)
if repository.checkBinaryAvailable()
if repository.checkBinaryAvailable() and repository.exists()
return repository
else
return null
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@
"0.1.0": "getRepositoryProviderService"
}
}
},
"devDependencies": {
"chai": "^3.5.0",
"coffee-script": "^1.10.0",
"mocha": "^3.0.2"
},
"scripts": {
"test": "mocha --compilers coffee:coffee-script/register"
}
}
23 changes: 23 additions & 0 deletions test/empty_repo.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'coffee-script/register'
require './fakeWindow'
HgRepository = require '../lib/hg-repository'
TestRepository = require './testRepository'
assert = require('chai').assert

describe 'Constructing hg-repository', ->
testRepo = new TestRepository 'empty_repo'

before ->
testRepo.init()

it 'should throw exception on nonexisting repository', ->
assert.throws ->
repo = new HgRepository (testRepo.fullPath() + "_not_exists")
, 'No Mercurial repository found searching path: ' + testRepo.fullPath()

it 'should create a repo from an empty repository', ->
repo = new HgRepository testRepo.fullPath()
assert.ok repo

after ->
testRepo.destroy()
1 change: 1 addition & 0 deletions test/empty_repo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hg init $args[0]
2 changes: 2 additions & 0 deletions test/empty_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
hg init $1
4 changes: 4 additions & 0 deletions test/fakeWindow.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global.window = {
addEventListener: ()->
removeEventListener: ()->
}
34 changes: 34 additions & 0 deletions test/simple_repo.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'coffee-script/register'
require './fakeWindow'
HgRepository = require '../lib/hg-repository'
TestRepository = require './testRepository'
assert = require('chai').assert
path = require 'path'

describe 'In a repo with some ignored files', ->
testRepo = new TestRepository path.parse(__filename).name
repo = null
before ->
testRepo.init()

beforeEach ->
repo = new HgRepository testRepo.fullPath()

describe 'with an ignored file', ->
ignored_file = path.join testRepo.fullPath(), 'ignored_file'

it 'should return isPathIgnored true', ->
repo.refreshStatus().then ->
assert.equal(repo.isPathIgnored(ignored_file), true)

it 'should return getPathStatus 0', ->
assert.equal(repo.getPathStatus(ignored_file), 0)

describe 'with a tracked file', ->
clean_file = path.join testRepo.fullPath(), 'clean_file'
it 'should return isPathIgnored false', ->
repo.refreshStatus().then ->
assert.equal(repo.isPathIgnored(clean_file), false)

after ->
testRepo.destroy()
21 changes: 21 additions & 0 deletions test/simple_repo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
hg init $args[0]
set-location $args[0]

("clean_file", "modified_file", "removed_file", "missing_file") | foreach-object {
new-item -type File $_
hg add $_
}

new-item -type File "untracked_file"
new-item -type File "ignored_file"

Set-Content -Path ".hgignore" -Value @"
syntax:glob
ignored_file
"@
hg add ".hgignore"

hg commit -m "Commit 1" -u "Tester <[email protected]>"
Set-Content -Path "modified_file" -Value "Changes!"
hg remove "removed_file"
Remove-Item "missing_file"
20 changes: 20 additions & 0 deletions test/simple_repo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
hg init $1
cd $1

files=("clean_file" "modified_file" "removed_file" "missing_file")
for f in ${files[*]}
do
touch $f
hg add $f
done
touch "untracked_file"
touch "ignored_file"

echo -e "syntax:glob\nignored_file">".hgignore"
hg add ".hgignore"

hg commit -m "Commit 1" -u "Tester <[email protected]>"
echo -e "Changes!">"modified_file"
hg remove "removed_file"
rm "missing_file"
30 changes: 30 additions & 0 deletions test/testRepository.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
path = require 'path'
fs = require 'fs'
exec = require('child_process').execSync

module.exports =
class TestRepository
isWindows = process.platform == 'win32'
extension = if isWindows then '.ps1' else '.sh'

constructor: (@scenario) ->

init: ->
if @exists()
@destroy()
if isWindows
exec 'powershell -file ' + path.join __dirname, @scenario + extension + ' ' + @fullPath()
else
exec path.join __dirname, @scenario + extension + ' ' + @fullPath()

fullPath: ->
path.join __dirname, 'test_repo'

exists: () ->
fs.existsSync(@fullPath)

destroy: ->
if isWindows
exec 'powershell -command "remove-item -recurse -force ' + @fullPath() + '"'
else
exec 'rm -rf ' + @fullPath()

0 comments on commit da37ad7

Please sign in to comment.