Skip to content

Commit

Permalink
Write a wrapper to eliminate done callbacks from async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kiskoza committed May 31, 2024
1 parent 35655e4 commit ea534dd
Show file tree
Hide file tree
Showing 46 changed files with 841 additions and 2,267 deletions.
124 changes: 31 additions & 93 deletions spec/atom-environment-spec.js

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions spec/atom-paths-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ describe('AtomPaths', () => {
expect(process.env.ATOM_HOME).toEqual(portableAtomHomePath);
});

it('uses ATOM_HOME if no write access to portable .atom folder', () => {
jasmine.filterByPlatform({except: ['win32']});
it('uses ATOM_HOME if no write access to portable .atom folder', (done) => {
jasmine.filterByPlatform({except: ['win32']}, done);

const readOnlyPath = temp.mkdirSync('atom-path-spec-no-write-access');
process.env.ATOM_HOME = readOnlyPath;
fs.chmodSync(portableAtomHomePath, 444);
atomPaths.setAtomHome(app.getPath('home'));
expect(process.env.ATOM_HOME).toEqual(readOnlyPath);

done();
});
});

Expand Down Expand Up @@ -119,14 +121,16 @@ describe('AtomPaths', () => {
expect(app.getPath('userData')).toEqual(electronUserDataPath);
});

it('leaves userData unchanged if no write access to electronUserData folder', () => {
jasmine.filterByPlatform({except: ['win32']});
it('leaves userData unchanged if no write access to electronUserData folder', (done) => {
jasmine.filterByPlatform({except: ['win32']}, done);

fs.mkdirSync(electronUserDataPath);
fs.chmodSync(electronUserDataPath, 444);
atomPaths.setUserData(app);
fs.chmodSync(electronUserDataPath, 666);
expect(app.getPath('userData')).toEqual(defaultElectronUserDataPath);

done();
});
});

Expand Down
8 changes: 2 additions & 6 deletions spec/buffered-node-process-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const BufferedNodeProcess = require('../src/buffered-node-process');

describe('BufferedNodeProcess', function() {
it('executes the script in a new process', async function(done) {
it('executes the script in a new process', async function() {
let output = '';
const stdout = lines => (output += lines);
let error = '';
Expand All @@ -18,11 +18,9 @@ describe('BufferedNodeProcess', function() {
expect(output).toBe('hi');
expect(error).toBe('');
expect(args).toEqual(['hi']);

done();
});

it('suppresses deprecations in the new process', async function(done) {
it('suppresses deprecations in the new process', async function() {
const exit = jasmine.createSpy('exitCallback');
let output = '';
const stdout = lines => (output += lines);
Expand All @@ -40,7 +38,5 @@ describe('BufferedNodeProcess', function() {

expect(output).toBe('hi');
expect(error).toBe('');

done();
});
});
22 changes: 6 additions & 16 deletions spec/buffered-process-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('BufferedProcess', function() {

describe('when there is an error handler specified', function() {
describe('when an error event is emitted by the process', () =>
it('calls the error handler and does not throw an exception', async function(done) {
it('calls the error handler and does not throw an exception', async function() {
const bufferedProcess = new BufferedProcess({
command: 'bad-command-nope1',
args: ['nothing'],
Expand All @@ -36,12 +36,10 @@ describe('BufferedProcess', function() {
expect(errorSpy.calls.mostRecent().args[0].error.message).toContain(
'spawn bad-command-nope1 ENOENT'
);

done();
}));

describe('when an error is thrown spawning the process', () =>
it('calls the error handler and does not throw an exception', async function(done) {
it('calls the error handler and does not throw an exception', async function() {
spyOn(ChildProcess, 'spawn').and.callFake(function() {
const error = new Error('Something is really wrong');
error.code = 'EAGAIN';
Expand Down Expand Up @@ -69,13 +67,11 @@ describe('BufferedProcess', function() {
expect(errorSpy.calls.mostRecent().args[0].error.message).toContain(
'Something is really wrong'
);

done();
}));
});

describe('when there is not an error handler specified', () =>
it('does throw an exception', async function(done) {
it('does throw an exception', async function() {
await new Promise((resolve) => {
window.onerror.and.callFake(resolve);

Expand All @@ -93,8 +89,6 @@ describe('BufferedProcess', function() {
expect(window.onerror.calls.mostRecent().args[4].name).toBe(
'BufferedProcessError'
);

done();
}));
});

Expand All @@ -103,7 +97,7 @@ describe('BufferedProcess', function() {
* TODO: FAILING TEST - This test fails with the following output:
* timeout: timed out after 120000 msec waiting for condition
*/
xit('doesnt start unless start method is called', async function(done) {
xit('doesnt start unless start method is called', async function() {
let stdout = '';
let stderr = '';
const exitCallback = jasmine.createSpy('exit callback');
Expand Down Expand Up @@ -134,14 +128,12 @@ describe('BufferedProcess', function() {

expect(stderr).toContain('apm - Atom Package Manager');
expect(stdout).toEqual('');

done();
}));
/**
* TODO: FAILING TEST - This test fails with the following output:
* timeout: timed out after 120000 msec waiting for condition
*/
xit('calls the specified stdout, stderr, and exit callbacks', async function(done) {
xit('calls the specified stdout, stderr, and exit callbacks', async function() {
let stdout = '';
let stderr = '';

Expand All @@ -164,7 +156,7 @@ describe('BufferedProcess', function() {
expect(stdout).toEqual('');
});

it('calls the specified stdout callback with whole lines', async function(done) {
it('calls the specified stdout callback with whole lines', async function() {
const exitCallback = jasmine.createSpy('exit callback');
const loremPath = require.resolve('./fixtures/lorem.txt');
const content = fs.readFileSync(loremPath).toString();
Expand All @@ -191,8 +183,6 @@ describe('BufferedProcess', function() {

expect(allLinesEndWithNewline).toBe(true);
expect(stdout).toBe(content);

done();
});

describe('on Windows', function() {
Expand Down
6 changes: 2 additions & 4 deletions spec/command-registry-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe('CommandRegistry', () => {
expect(registry.dispatch(parent, 'command')).toBe(null);
});

it('returns a promise that resolves when the listeners resolve', async (done) => {
it('returns a promise that resolves when the listeners resolve', async () => {
jasmine.useRealClock();
registry.add('.grandchild', 'command', () => 1);
registry.add('.grandchild', 'command', () => Promise.resolve(2));
Expand All @@ -404,10 +404,9 @@ describe('CommandRegistry', () => {

const values = await registry.dispatch(grandchild, 'command');
expect(values).toEqual([3, 2, 1]);
done();
});

it('returns a promise that rejects when a listener is rejected', async (done) => {
it('returns a promise that rejects when a listener is rejected', async () => {
jasmine.useRealClock();
registry.add('.grandchild', 'command', () => 1);
registry.add('.grandchild', 'command', () => Promise.resolve(2));
Expand All @@ -429,7 +428,6 @@ describe('CommandRegistry', () => {
value = err;
}
expect(value).toBe(3);
done();
});
});

Expand Down
16 changes: 4 additions & 12 deletions spec/config-file-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,24 @@ describe('ConfigFile', () => {
});

describe('when the file does not exist', () => {
it('returns an empty object from .get()', async (done) => {
it('returns an empty object from .get()', async () => {
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
expect(configFile.get()).toEqual({});

done();
});
});

describe('when the file is empty', () => {
it('returns an empty object from .get()', async (done) => {
it('returns an empty object from .get()', async () => {
writeFileSync(filePath, '');
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();
expect(configFile.get()).toEqual({});

done();
});
});

describe('when the file is updated with valid CSON', () => {
it('notifies onDidChange observers with the data', async (done) => {
it('notifies onDidChange observers with the data', async () => {
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();

Expand All @@ -65,13 +61,11 @@ describe('ConfigFile', () => {
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
});

done();
});
});

describe('when the file is updated with invalid CSON', () => {
it('notifies onDidError observers', async (done) => {
it('notifies onDidError observers', async () => {
configFile = new ConfigFile(filePath);
subscription = await configFile.watch();

Expand Down Expand Up @@ -105,8 +99,6 @@ describe('ConfigFile', () => {
'*': { foo: 'bar' },
javascript: { foo: 'baz' }
});

done();
});
});

Expand Down
12 changes: 3 additions & 9 deletions spec/config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ describe('Config', () => {
atom.config.onDidChange('foo.bar.baz', changeSpy);
});

it('allows only one change event for the duration of the given promise if it gets resolved', async (done) => {
it('allows only one change event for the duration of the given promise if it gets resolved', async () => {
let promiseResult = null;
const transactionPromise = atom.config.transactAsync(() => {
atom.config.set('foo.bar.baz', 1);
Expand All @@ -1059,11 +1059,9 @@ describe('Config', () => {
newValue: 3,
oldValue: undefined
});

done();
});

it('allows only one change event for the duration of the given promise if it gets rejected', async (done) => {
it('allows only one change event for the duration of the given promise if it gets rejected', async () => {
let promiseError = null;
const transactionPromise = atom.config.transactAsync(() => {
atom.config.set('foo.bar.baz', 1);
Expand All @@ -1082,11 +1080,9 @@ describe('Config', () => {
newValue: 3,
oldValue: undefined
});

done();
});

it('allows only one change event even when the given callback throws', async (done) => {
it('allows only one change event even when the given callback throws', async () => {
const error = new Error('Oops!');
let promiseError = null;
const transactionPromise = atom.config.transactAsync(() => {
Expand All @@ -1106,8 +1102,6 @@ describe('Config', () => {
newValue: 3,
oldValue: undefined
});

done();
});
});

Expand Down
4 changes: 1 addition & 3 deletions spec/decoration-manager-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ const TextEditor = require('../src/text-editor');
describe('DecorationManager', function() {
let decorationManager, buffer, editor, markerLayer1, markerLayer2;

beforeEach(async function(done) {
beforeEach(async function() {
buffer = atom.project.bufferForPathSync('sample.js');
editor = new TextEditor({ buffer });
markerLayer1 = editor.addMarkerLayer();
markerLayer2 = editor.addMarkerLayer();
decorationManager = new DecorationManager(editor);

await atom.packages.activatePackage('language-javascript');

done();
});

afterEach(() => buffer.destroy());
Expand Down
10 changes: 5 additions & 5 deletions spec/default-directory-provider-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ describe('DefaultDirectoryProvider', function() {
expect(directory.getPath()).toEqual(tmp);
});

it('normalizes disk drive letter in path on win32', function() {
jasmine.filterByPlatform({only: ['win32']});
it('normalizes disk drive letter in path on win32', function(done) {
jasmine.filterByPlatform({only: ['win32']}, done);

const provider = new DefaultDirectoryProvider();
const nonNormalizedPath = tmp[0].toLowerCase() + tmp.slice(1);
Expand All @@ -43,6 +43,8 @@ describe('DefaultDirectoryProvider', function() {

const directory = provider.directoryForURISync(nonNormalizedPath);
expect(directory.getPath()).toEqual(tmp);

done()
});

it('creates a Directory for its parent dir when passed a file', function() {
Expand All @@ -63,12 +65,10 @@ describe('DefaultDirectoryProvider', function() {
});

describe('.directoryForURI(uri)', () =>
it('returns a Promise that resolves to a Directory with a path that matches the uri', async function(done) {
it('returns a Promise that resolves to a Directory with a path that matches the uri', async function() {
const provider = new DefaultDirectoryProvider();

let directory = await provider.directoryForURI(tmp);
expect(directory.getPath()).toEqual(tmp);

done();
}));
});
4 changes: 1 addition & 3 deletions spec/default-directory-searcher-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('DefaultDirectorySearcher', function() {
searcher = new DefaultDirectorySearcher();
});

it('terminates the task after running a search', async function(done) {
it('terminates the task after running a search', async function() {
const options = {
ignoreCase: false,
includeHidden: false,
Expand All @@ -38,7 +38,5 @@ describe('DefaultDirectorySearcher', function() {
);

expect(Task.prototype.terminate).toHaveBeenCalled();

done();
});
});
Loading

0 comments on commit ea534dd

Please sign in to comment.