Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit aab456b

Browse files
author
Winston Liu
authored
Merge pull request #1268 from atom/wl-repo-for-path-specs
Add specs for repoForPath
2 parents 48b7cae + bbf28b7 commit aab456b

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

spec/async-spec-helpers.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/** @babel */
2+
3+
export function beforeEach (fn) {
4+
global.beforeEach(function () {
5+
const result = fn()
6+
if (result instanceof Promise) {
7+
waitsForPromise(() => result)
8+
}
9+
})
10+
}
11+
12+
export function afterEach (fn) {
13+
global.afterEach(function () {
14+
const result = fn()
15+
if (result instanceof Promise) {
16+
waitsForPromise(() => result)
17+
}
18+
})
19+
}
20+
21+
['it', 'fit', 'ffit', 'fffit'].forEach(function (name) {
22+
module.exports[name] = function (description, fn) {
23+
if (fn === undefined) {
24+
global[name](description)
25+
return
26+
}
27+
28+
global[name](description, function () {
29+
const result = fn()
30+
if (result instanceof Promise) {
31+
waitsForPromise(() => result)
32+
}
33+
})
34+
}
35+
})
36+
37+
export async function conditionPromise (condition, description = 'anonymous condition') {
38+
const startTime = Date.now()
39+
40+
while (true) {
41+
await timeoutPromise(100)
42+
43+
if (await condition()) {
44+
return
45+
}
46+
47+
if (Date.now() - startTime > 5000) {
48+
throw new Error('Timed out waiting on ' + description)
49+
}
50+
}
51+
}
52+
53+
export function timeoutPromise (timeout) {
54+
return new Promise(function (resolve) {
55+
global.setTimeout(resolve, timeout)
56+
})
57+
}
58+
59+
function waitsForPromise (fn) {
60+
const promise = fn()
61+
global.waitsFor('spec promise to resolve', function (done) {
62+
promise.then(done, function (error) {
63+
jasmine.getEnv().currentSpec.fail(error)
64+
done()
65+
})
66+
})
67+
}
68+
69+
export function emitterEventPromise (emitter, event, timeout = 15000) {
70+
return new Promise((resolve, reject) => {
71+
const timeoutHandle = setTimeout(() => {
72+
reject(new Error(`Timed out waiting for '${event}' event`))
73+
}, timeout)
74+
emitter.once(event, () => {
75+
clearTimeout(timeoutHandle)
76+
resolve()
77+
})
78+
})
79+
}
80+
81+
export function promisify (original) {
82+
return function (...args) {
83+
return new Promise((resolve, reject) => {
84+
args.push((err, ...results) => {
85+
if (err) {
86+
reject(err)
87+
} else {
88+
resolve(...results)
89+
}
90+
})
91+
92+
return original(...args)
93+
})
94+
}
95+
}
96+
97+
export function promisifySome (obj, fnNames) {
98+
const result = {}
99+
for (const fnName of fnNames) {
100+
result[fnName] = promisify(obj[fnName])
101+
}
102+
return result
103+
}

spec/helpers-spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1+
const {it, fit, ffit, beforeEach, afterEach} = require('./async-spec-helpers') // eslint-disable-line no-unused-vars
2+
13
const path = require('path')
24

35
const helpers = require('../lib/helpers')
46

57
describe('Helpers', () => {
8+
describe('repoForPath', () => {
9+
let fixturesPath, fixturesRepo
10+
11+
beforeEach(async () => {
12+
fixturesPath = atom.project.getPaths()[0]
13+
fixturesRepo = await atom.project.repositoryForDirectory(atom.project.getDirectories()[0])
14+
})
15+
16+
it('returns the repository for a given project path', () => {
17+
expect(helpers.repoForPath(fixturesPath)).toEqual(fixturesRepo)
18+
})
19+
20+
it('returns the project repository for a subpath', () => {
21+
expect(helpers.repoForPath(path.join(fixturesPath, 'root-dir1', 'tree-view.txt'))).toEqual(fixturesRepo)
22+
})
23+
24+
it('returns null for a path outside the project', () => {
25+
expect(helpers.repoForPath(path.join(fixturesPath, '..'))).toEqual(null)
26+
})
27+
})
28+
629
describe('getFullExtension', () => {
730
it('returns the extension for a simple file', () => {
831
expect(helpers.getFullExtension('filename.txt')).toBe('.txt')

0 commit comments

Comments
 (0)