Skip to content

Commit

Permalink
fix: resolve sfdx project path before checking cache
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro committed Oct 12, 2020
1 parent 8cba4d1 commit 265e523
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/sfdxProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ export class SfdxProject {
* **Throws** *{@link SfdxError}{ name: 'InvalidProjectWorkspace' }* If the current folder is not located in a workspace.
*/
public static async resolve(path?: string): Promise<SfdxProject> {
path = path || process.cwd();
path = await this.resolveProjectPath(path || process.cwd());
if (!SfdxProject.instances.has(path)) {
const project = new SfdxProject(await this.resolveProjectPath(path));
const project = new SfdxProject(path);
SfdxProject.instances.set(path, project);
}
return ensure(SfdxProject.instances.get(path));
Expand All @@ -371,9 +371,11 @@ export class SfdxProject {
* **Throws** *{@link SfdxError}{ name: 'InvalidProjectWorkspace' }* If the current folder is not located in a workspace.
*/
public static getInstance(path?: string): SfdxProject {
path = path || process.cwd();
// Store instance based on the path of the actual project.
path = this.resolveProjectPathSync(path || process.cwd());

if (!SfdxProject.instances.has(path)) {
const project = new SfdxProject(this.resolveProjectPathSync(path));
const project = new SfdxProject(path);
SfdxProject.instances.set(path, project);
}
return ensure(SfdxProject.instances.get(path));
Expand Down
41 changes: 41 additions & 0 deletions test/unit/projectTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ describe('SfdxProject', () => {
const sfdxProject = await project.retrieveSfdxProjectJson();
expect(sfdxProject.getPath()).to.equal(`/path/${SfdxProjectJson.getFileName()}`);
});
it('with path in project', async () => {
$$.SANDBOXES.PROJECT.restore();
const resolveStub = $$.SANDBOX.stub(SfdxProject, 'resolveProjectPath');
resolveStub.onFirstCall().resolves('/path');
resolveStub.onSecondCall().resolves('/path');
const project1 = await SfdxProject.resolve('/path');
const project2 = await SfdxProject.resolve('/path/in/side/project');
expect(project2.getPath()).to.equal('/path');
expect(project1).to.equal(project2);
});
it('with path throws with no sfdx-project.json', async () => {
$$.SANDBOXES.PROJECT.restore();
$$.SANDBOX.stub(SfdxProject, 'resolveProjectPath').throws(new Error('InvalidProjectWorkspace'));
Expand All @@ -242,6 +252,37 @@ describe('SfdxProject', () => {
});
});

describe('getInstance', () => {
it('with path', async () => {
$$.SANDBOXES.PROJECT.restore();
$$.SANDBOX.stub(SfdxProject, 'resolveProjectPathSync').returns('/path');
const project = SfdxProject.getInstance('/path');
expect(project.getPath()).to.equal('/path');
const sfdxProject = await project.retrieveSfdxProjectJson();
expect(sfdxProject.getPath()).to.equal(`/path/${SfdxProjectJson.getFileName()}`);
});
it('with path in project', async () => {
$$.SANDBOXES.PROJECT.restore();
const resolveStub = $$.SANDBOX.stub(SfdxProject, 'resolveProjectPathSync');
resolveStub.onFirstCall().returns('/path');
resolveStub.onSecondCall().returns('/path');
const project1 = SfdxProject.getInstance('/path');
const project2 = SfdxProject.getInstance('/path/in/side/project');
expect(project2.getPath()).to.equal('/path');
expect(project1).to.equal(project2);
});
it('with path throws with no sfdx-project.json', async () => {
$$.SANDBOXES.PROJECT.restore();
$$.SANDBOX.stub(SfdxProject, 'resolveProjectPathSync').throws(new Error('InvalidProjectWorkspace'));
try {
SfdxProject.getInstance();
assert.fail();
} catch (e) {
expect(e.message).to.equal('InvalidProjectWorkspace');
}
});
});

describe('resolveProjectConfig', () => {
beforeEach(() => {
delete process.env.FORCE_SFDC_LOGIN_URL;
Expand Down

0 comments on commit 265e523

Please sign in to comment.