Skip to content

Commit

Permalink
WIP: Fix spec files running with jasmine 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kiskoza committed May 6, 2024
1 parent d67ed90 commit 3f5b584
Show file tree
Hide file tree
Showing 53 changed files with 3,992 additions and 3,101 deletions.
213 changes: 137 additions & 76 deletions spec/atom-environment-spec.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions spec/atom-protocol-handler-spec.js
@@ -1,10 +1,10 @@
describe('"atom" protocol URL', () =>
it('sends the file relative in the package as response', function() {
let called = false;
describe('"atom" protocol URL', () => {
it('sends the file relative in the package as response', (done) => {
const request = new XMLHttpRequest();
request.addEventListener('load', () => (called = true));
request.addEventListener('load', () => {
done();
});
request.open('GET', 'atom://async/package.json', true);
request.send();

waitsFor('request to be done', () => called === true);
}));
})
});
31 changes: 15 additions & 16 deletions spec/buffered-node-process-spec.js
Expand Up @@ -3,27 +3,26 @@ const path = require('path');
const BufferedNodeProcess = require('../src/buffered-node-process');

describe('BufferedNodeProcess', function() {
it('executes the script in a new process', function() {
const exit = jasmine.createSpy('exitCallback');
it('executes the script in a new process', async function(done) {
let output = '';
const stdout = lines => (output += lines);
let error = '';
const stderr = lines => (error += lines);
const args = ['hi'];
const command = path.join(__dirname, 'fixtures', 'script.js');

new BufferedNodeProcess({ command, args, stdout, stderr, exit });
await new Promise((resolve) => {
new BufferedNodeProcess({ command, args, stdout, stderr, exit: resolve });
})

waitsFor(() => exit.callCount === 1);
expect(output).toBe('hi');
expect(error).toBe('');
expect(args).toEqual(['hi']);

runs(function() {
expect(output).toBe('hi');
expect(error).toBe('');
expect(args).toEqual(['hi']);
});
done();
});

it('suppresses deprecations in the new process', function() {
it('suppresses deprecations in the new process', async function(done) {
const exit = jasmine.createSpy('exitCallback');
let output = '';
const stdout = lines => (output += lines);
Expand All @@ -35,13 +34,13 @@ describe('BufferedNodeProcess', function() {
'script-with-deprecations.js'
);

new BufferedNodeProcess({ command, stdout, stderr, exit });
await new Promise((resolve) => {
new BufferedNodeProcess({ command, stdout, stderr, exit: resolve });
})

waitsFor(() => exit.callCount === 1);
expect(output).toBe('hi');
expect(error).toBe('');

runs(function() {
expect(output).toBe('hi');
expect(error).toBe('');
});
done();
});
});
211 changes: 112 additions & 99 deletions spec/buffered-process-spec.js
Expand Up @@ -6,42 +6,43 @@ const BufferedProcess = require('../src/buffered-process');

describe('BufferedProcess', function() {
describe('when a bad command is specified', function() {
let [oldOnError] = [];
let windowOnErrorSpy;

beforeEach(function() {
oldOnError = window.onerror;
window.onerror = jasmine.createSpy();
windowOnErrorSpy = spyOn(window, 'onerror');
});

afterEach(() => (window.onerror = oldOnError));

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', function() {
it('calls the error handler and does not throw an exception', async function(done) {
const bufferedProcess = new BufferedProcess({
command: 'bad-command-nope1',
args: ['nothing'],
options: { shell: false }
});

const errorSpy = jasmine
.createSpy()
.andCallFake(error => error.handle());
bufferedProcess.onWillThrowError(errorSpy);
const errorSpy = jasmine.createSpy()

waitsFor(() => errorSpy.callCount > 0);
await new Promise((resolve) => {
errorSpy.and.callFake((error) => {
error.handle();
resolve();
});
bufferedProcess.onWillThrowError(errorSpy);;
})

runs(function() {
expect(window.onerror).not.toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
expect(errorSpy.mostRecentCall.args[0].error.message).toContain(
'spawn bad-command-nope1 ENOENT'
);
});
expect(window.onerror).not.toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
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', function() {
spyOn(ChildProcess, 'spawn').andCallFake(function() {
it('calls the error handler and does not throw an exception', async function(done) {
spyOn(ChildProcess, 'spawn').and.callFake(function() {
const error = new Error('Something is really wrong');
error.code = 'EAGAIN';
throw error;
Expand All @@ -53,42 +54,47 @@ describe('BufferedProcess', function() {
options: {}
});

const errorSpy = jasmine
.createSpy()
.andCallFake(error => error.handle());
bufferedProcess.onWillThrowError(errorSpy);
const errorSpy = jasmine.createSpy();

waitsFor(() => errorSpy.callCount > 0);
await new Promise((resolve) => {
errorSpy.and.callFake(error => {
error.handle();
resolve();
});
bufferedProcess.onWillThrowError(errorSpy);
})

runs(function() {
expect(window.onerror).not.toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
expect(errorSpy.mostRecentCall.args[0].error.message).toContain(
'Something is really wrong'
);
});
expect(window.onerror).not.toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
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', function() {
new BufferedProcess({
command: 'bad-command-nope2',
args: ['nothing'],
options: { shell: false }
it('does throw an exception', async function(done) {
await new Promise((resolve) => {
window.onerror.and.callFake(resolve);

new BufferedProcess({
command: 'bad-command-nope2',
args: ['nothing'],
options: {shell: false}
});
});

waitsFor(() => window.onerror.callCount > 0);
expect(window.onerror).toHaveBeenCalled();
expect(window.onerror.calls.mostRecent().args[0]).toContain(
'Failed to spawn command `bad-command-nope2`'
);
expect(window.onerror.calls.mostRecent().args[4].name).toBe(
'BufferedProcessError'
);

runs(function() {
expect(window.onerror).toHaveBeenCalled();
expect(window.onerror.mostRecentCall.args[0]).toContain(
'Failed to spawn command `bad-command-nope2`'
);
expect(window.onerror.mostRecentCall.args[4].name).toBe(
'BufferedProcessError'
);
});
done();
}));
});

Expand All @@ -97,7 +103,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', function() {
xit('doesnt start unless start method is called', async function(done) {
let stdout = '';
let stderr = '';
const exitCallback = jasmine.createSpy('exit callback');
Expand All @@ -116,70 +122,77 @@ describe('BufferedProcess', function() {
});

expect(apmProcess.started).not.toBe(true);
apmProcess.start();
expect(apmProcess.started).toBe(true);

waitsFor(() => exitCallback.callCount === 1);
runs(function() {
expect(stderr).toContain('apm - Atom Package Manager');
expect(stdout).toEqual('');
});
await new Promise((resolve) => {
exitCallback.and.callFake(() => {
expect(apmProcess.started).toBe(true);
resolve();
})

apmProcess.start();
})

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
* timeout: timed out after 120000 msec waiting for condition
*/
xit('calls the specified stdout, stderr, and exit callbacks', function() {
xit('calls the specified stdout, stderr, and exit callbacks', async function(done) {
let stdout = '';
let stderr = '';
const exitCallback = jasmine.createSpy('exit callback');
new BufferedProcess({
command: atom.packages.getApmPath(),
args: ['-h'],
options: {},
stdout(lines) {
stdout += lines;
},
stderr(lines) {
stderr += lines;
},
exit: exitCallback
});

waitsFor(() => exitCallback.callCount === 1);
await new Promise((resolve) => {
new BufferedProcess({
command: atom.packages.getApmPath(),
args: ['-h'],
options: {},
stdout(lines) {
stdout += lines;
},
stderr(lines) {
stderr += lines;
},
exit: resolve
});
})

runs(function() {
expect(stderr).toContain('apm - Atom Package Manager');
expect(stdout).toEqual('');
});
expect(stderr).toContain('apm - Atom Package Manager');
expect(stdout).toEqual('');
});

it('calls the specified stdout callback with whole lines', function() {
it('calls the specified stdout callback with whole lines', async function(done) {
const exitCallback = jasmine.createSpy('exit callback');
const loremPath = require.resolve('./fixtures/lorem.txt');
const content = fs.readFileSync(loremPath).toString();
let stdout = '';
let allLinesEndWithNewline = true;
new BufferedProcess({
command: process.platform === 'win32' ? 'type' : 'cat',
args: [loremPath],
options: {},
stdout(lines) {
const endsWithNewline = lines.charAt(lines.length - 1) === '\n';
if (!endsWithNewline) {
allLinesEndWithNewline = false;
}
stdout += lines;
},
exit: exitCallback
});

waitsFor(() => exitCallback.callCount === 1);
await new Promise((resolve) => {
exitCallback.and.callFake(resolve)

runs(function() {
expect(allLinesEndWithNewline).toBe(true);
expect(stdout).toBe(content);
new BufferedProcess({
command: process.platform === 'win32' ? 'type' : 'cat',
args: [loremPath],
options: {},
stdout(lines) {
const endsWithNewline = lines.charAt(lines.length - 1) === '\n';
if (!endsWithNewline) {
allLinesEndWithNewline = false;
}
stdout += lines;
},
exit: exitCallback
});
});

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

done();
});

describe('on Windows', function() {
Expand All @@ -202,20 +215,20 @@ describe('BufferedProcess', function() {
command: 'explorer.exe',
args: ['/root,C:\\foo']
});
expect(ChildProcess.spawn.argsForCall[0][1][3]).toBe(
expect(ChildProcess.spawn.calls.argsFor(0)[1][3]).toBe(
'"explorer.exe /root,C:\\foo"'
);
}));

it('spawns the command using a cmd.exe wrapper when options.shell is undefined', function() {
new BufferedProcess({ command: 'dir' });
expect(path.basename(ChildProcess.spawn.argsForCall[0][0])).toBe(
expect(path.basename(ChildProcess.spawn.calls.argsFor(0)[0])).toBe(
'cmd.exe'
);
expect(ChildProcess.spawn.argsForCall[0][1][0]).toBe('/s');
expect(ChildProcess.spawn.argsForCall[0][1][1]).toBe('/d');
expect(ChildProcess.spawn.argsForCall[0][1][2]).toBe('/c');
expect(ChildProcess.spawn.argsForCall[0][1][3]).toBe('"dir"');
expect(ChildProcess.spawn.calls.argsFor(0)[1][0]).toBe('/s');
expect(ChildProcess.spawn.calls.argsFor(0)[1][1]).toBe('/d');
expect(ChildProcess.spawn.calls.argsFor(0)[1][2]).toBe('/c');
expect(ChildProcess.spawn.calls.argsFor(0)[1][3]).toBe('"dir"');
});
});
});

0 comments on commit 3f5b584

Please sign in to comment.