Skip to content

Commit

Permalink
test: result finder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saintsebastian committed Aug 24, 2018
1 parent b661fc1 commit 07e7926
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 139 deletions.
5 changes: 2 additions & 3 deletions src/firefox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,10 @@ export function defaultCreateProfileFinder(
return await getPath(profileName);
}
} catch (error) {
if (isErrorWithCode('ENOENT', error)) {
log.warn('No firefox profiles exist');
} else {
if (!isErrorWithCode('ENOENT', error)) {
throw error;
}
log.warn('Unable to find Firefox profiles.ini');
}
};
}
Expand Down
326 changes: 190 additions & 136 deletions tests/unit/test-firefox/test.firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ import {
fake,
makeSureItFails,
TCPConnectError,
ErrorWithCode,
} from '../helpers';
import {manifestWithoutApps} from '../test-util/test.manifest';
import {RemoteFirefox} from '../../../src/firefox/remote';
import {
consoleStream, // instance is imported to inspect logged messages
} from '../../../src/util/logger';
import type {RemotePortFinderParams} from '../../../src/firefox/index';

const {defaultFirefoxEnv} = firefox;
Expand Down Expand Up @@ -546,101 +550,85 @@ describe('firefox', () => {

it('resolves to a FirefoxProfile instance', () => withBaseProfile(
async (baseProfile) => {
try {
const app = 'fennec';
const configureThisProfile = (profile) => Promise.resolve(profile);
const createProfileFinder = () => {
return (profilePath) => Promise.resolve(profilePath);
};
const profile = await firefox.useProfile(baseProfile.path(), {
app,
configureThisProfile,
createProfileFinder,
});
assert.instanceOf(profile, FirefoxProfile);
} catch (error) {
throw error;
}
const app = 'fennec';
const configureThisProfile = (profile) => Promise.resolve(profile);
const createProfileFinder = () => {
return (profilePath) => Promise.resolve(profilePath);
};
const profile = await firefox.useProfile(baseProfile.path(), {
app,
configureThisProfile,
createProfileFinder,
});
assert.instanceOf(profile, FirefoxProfile);
}
));

it('looks for profile path if passed a name', () => withBaseProfile(
async (baseProfile) => {
try {
const app = 'fennec';
const fakeGetProfilePath = sinon.spy(() => baseProfile.path());
const createProfileFinder = () => {
return fakeGetProfilePath;
};
const isFirefoxDefaultProfile = sinon.spy(
() => Promise.resolve(false)
);
await firefox.useProfile('profileName', {
app,
createProfileFinder,
isFirefoxDefaultProfile,
});
sinon.assert.calledOnce(fakeGetProfilePath);
sinon.assert.calledWith(
fakeGetProfilePath,
sinon.match('profileName')
);
} catch (error) {
throw error;
}
const app = 'fennec';
const fakeGetProfilePath = sinon.spy(() => baseProfile.path());
const createProfileFinder = () => {
return fakeGetProfilePath;
};
const isFirefoxDefaultProfile = sinon.spy(
() => Promise.resolve(false)
);
await firefox.useProfile('profileName', {
app,
createProfileFinder,
isFirefoxDefaultProfile,
});
sinon.assert.calledOnce(fakeGetProfilePath);
sinon.assert.calledWith(
fakeGetProfilePath,
sinon.match('profileName')
);
}
));

it('checks if named profile is default', () => withBaseProfile(
async (baseProfile) => {
try {
const app = 'fennec';
const createProfileFinder = () => {
return () => Promise.resolve(baseProfile.path());
};
const isFirefoxDefaultProfile = sinon.spy(
() => Promise.resolve(false)
);
await firefox.useProfile('profileName', {
app,
createProfileFinder,
isFirefoxDefaultProfile,
});
sinon.assert.calledOnce(isFirefoxDefaultProfile);
sinon.assert.calledWith(
isFirefoxDefaultProfile,
sinon.match('profileName')
);
} catch (error) {
throw error;
}
const app = 'fennec';
const createProfileFinder = () => {
return () => Promise.resolve(baseProfile.path());
};
const isFirefoxDefaultProfile = sinon.spy(
() => Promise.resolve(false)
);
await firefox.useProfile('profileName', {
app,
createProfileFinder,
isFirefoxDefaultProfile,
});
sinon.assert.calledOnce(isFirefoxDefaultProfile);
sinon.assert.calledWith(
isFirefoxDefaultProfile,
sinon.match('profileName')
);
}
));

it('checks if path leads to default profile', () => withBaseProfile(
async (baseProfile) => {
try {
const app = 'fennec';
const profilePath = baseProfile.path();
const createProfileFinder = () => {
return () => Promise.resolve(profilePath);
};
const isFirefoxDefaultProfile = sinon.spy(
() => Promise.resolve(false)
);
await firefox.useProfile(profilePath, {
app,
createProfileFinder,
isFirefoxDefaultProfile,
});
sinon.assert.calledOnce(isFirefoxDefaultProfile);
sinon.assert.calledWith(
isFirefoxDefaultProfile,
sinon.match(profilePath)
);
} catch (error) {
throw error;
}
const app = 'fennec';
const profilePath = baseProfile.path();
const createProfileFinder = () => {
return () => Promise.resolve(profilePath);
};
const isFirefoxDefaultProfile = sinon.spy(
() => Promise.resolve(false)
);
await firefox.useProfile(profilePath, {
app,
createProfileFinder,
isFirefoxDefaultProfile,
});
sinon.assert.calledOnce(isFirefoxDefaultProfile);
sinon.assert.calledWith(
isFirefoxDefaultProfile,
sinon.match(profilePath)
);
}
));

Expand All @@ -664,73 +652,139 @@ describe('firefox', () => {
describe('defaultCreateProfileFinder', () => {

it('creates a finder', async () => {
try {
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve()),
};
firefox.defaultCreateProfileFinder({FxProfile});
sinon.assert.called(FxProfile.Finder);
sinon.assert.calledWith(
FxProfile.Finder,
sinon.notOk,
);
} catch (error) {
throw error;
}
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve()),
};
firefox.defaultCreateProfileFinder({FxProfile});
sinon.assert.called(FxProfile.Finder);
sinon.assert.calledWith(
FxProfile.Finder,
sinon.notOk,
);
});

it('creates finder based on userDirectoryPath if present', async () => {
try {
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve()),
};
const userDirectoryPath = '/non/existent/path';
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve()),
};
const userDirectoryPath = '/non/existent/path';

firefox.defaultCreateProfileFinder({userDirectoryPath, FxProfile});
firefox.defaultCreateProfileFinder({userDirectoryPath, FxProfile});

sinon.assert.called(FxProfile.Finder);
sinon.assert.calledWith(
FxProfile.Finder,
sinon.match(userDirectoryPath),
);
} catch (error) {
throw error;
}
sinon.assert.called(FxProfile.Finder);
sinon.assert.calledWith(
FxProfile.Finder,
sinon.match(userDirectoryPath),
);
});

it('returns a function that looks for a default profile', async () => {
try {
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve({})),
};
const fakeReadProfiles = sinon.spy(() => Promise.resolve());
const fakeGetPath = sinon.spy(() => Promise.resolve());
FxProfile.Finder.prototype.readProfiles = fakeReadProfiles;
FxProfile.Finder.prototype.getPath = fakeGetPath;
FxProfile.Finder.prototype.profiles = [{
Name: 'someName',
}];

const userDirectoryPath = '/non/existent/path';

const getter = firefox.defaultCreateProfileFinder({
userDirectoryPath,
FxProfile,
});
it('returns a finder that looks for a default profile', async () => {
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve({})),
};
const fakeReadProfiles = sinon.spy(() => Promise.resolve());
const fakeGetPath = sinon.spy(() => Promise.resolve());
FxProfile.Finder.prototype.readProfiles = fakeReadProfiles;
FxProfile.Finder.prototype.getPath = fakeGetPath;
FxProfile.Finder.prototype.profiles = [{
Name: 'someName',
}];

const userDirectoryPath = '/non/existent/path';

const getter = firefox.defaultCreateProfileFinder({
userDirectoryPath,
FxProfile,
});

await getter('someName');
await getter('someName');

sinon.assert.called(fakeReadProfiles);
sinon.assert.called(fakeGetPath);
sinon.assert.calledWith(
fakeGetPath,
sinon.match('someName'),
);
} catch (error) {
throw error;
}
sinon.assert.called(fakeReadProfiles);
sinon.assert.called(fakeGetPath);
sinon.assert.calledWith(
fakeGetPath,
sinon.match('someName'),
);
});

it('returns a finder that resolves undefined for no profiles.ini',
async () => {
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve({})),
};

const fakeReadProfiles = sinon.spy(() => {
return Promise.reject(
new ErrorWithCode('ENOENT', 'fake ENOENT error'));
});
const fakeGetPath = sinon.spy(() => Promise.resolve());
FxProfile.Finder.prototype.readProfiles = fakeReadProfiles;
FxProfile.Finder.prototype.getPath = fakeGetPath;
FxProfile.Finder.prototype.profiles = [{
Name: 'someName',
}];

const userDirectoryPath = '/non/existent/path';

const getter = firefox.defaultCreateProfileFinder({
userDirectoryPath,
FxProfile,
});

consoleStream.startCapturing();

const res = await getter('someName');
assert.equal(
res,
undefined,
'Got an undefined result when the profiles.ini file does not exist');

assert.match(
consoleStream.capturedMessages[0],
'Unable to find Firefox profiles.ini'
);

consoleStream.flushCapturedLogs();
consoleStream.stopCapturing();
sinon.assert.called(fakeReadProfiles);
sinon.assert.notCalled(fakeGetPath);
});

it('returns a finder that throws other errors',
async () => {
const FxProfile = {
Finder: sinon.spy(() => () => Promise.resolve({})),
};

const fakeReadProfiles = sinon.spy(() => {
return Promise.reject(
new Error('unspecified error'));
});
const fakeGetPath = sinon.spy(() => Promise.resolve());
FxProfile.Finder.prototype.readProfiles = fakeReadProfiles;
FxProfile.Finder.prototype.getPath = fakeGetPath;
FxProfile.Finder.prototype.profiles = [{
Name: 'someName',
}];

const userDirectoryPath = '/non/existent/path';

const getter = firefox.defaultCreateProfileFinder({
userDirectoryPath,
FxProfile,
});

try {
await getter('someName');
} catch (e) {
assert.equal(
e.message,
'unspecified error',
'Throws expected error');
}
sinon.assert.called(fakeReadProfiles);
sinon.assert.notCalled(fakeGetPath);
});

});

Expand Down

0 comments on commit 07e7926

Please sign in to comment.