Skip to content

Commit

Permalink
refactor: narrower catch to just the fs read, plus ut for no file
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed May 31, 2023
1 parent 6181899 commit a6ff169
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/stateAggregator/accessors/aliasAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ export class AliasAccessor extends AsyncOptionalCreatable {
* if the file doesn't exist, create it empty
*/
private async readFileToAliasStore(useLock = false): Promise<void> {
if (useLock) {
await lock(this.fileLocation, lockRetryOptions);
}
try {
if (useLock) {
await lock(this.fileLocation, lockRetryOptions);
}
this.aliasStore = fileContentsRawToAliasStore(await readFile(this.fileLocation, 'utf-8'));
} catch (e) {
if (e instanceof Error && 'code' in e && e.code === 'ENOENT') {
Expand Down
23 changes: 22 additions & 1 deletion test/unit/stateAggregator/accessors/aliasAccessorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { join } from 'node:path';
import { existsSync } from 'node:fs';
import { rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { expect } from 'chai';
import { StateAggregator } from '../../../../src/stateAggregator';
import { FILENAME, StateAggregator } from '../../../../src/stateAggregator';
import { MockTestOrgData, TestContext, uniqid } from '../../../../src/testSetup';
import { Global } from '../../../../src/global';

const username1 = '[email protected]';
const username2 = '[email protected]';
Expand Down Expand Up @@ -174,6 +179,20 @@ describe('AliasAccessor', () => {
});
});

describe('lockfile concerns', () => {
it('no aliases file, creates empty file', async () => {
const fileLocation = getAliasFileLocation();
await rm(fileLocation);
expect(existsSync(fileLocation)).to.be.false;
const stateAggregator = await StateAggregator.getInstance();
const aliases = stateAggregator.aliases.getAll(username1);
expect(aliases).to.deep.equal([]);
const all = stateAggregator.aliases.getAll();
expect(all).to.deep.equal({});
expect(existsSync(fileLocation)).to.be.true;
});
});

describe('concurrent access', () => {
const quantity = 50;

Expand Down Expand Up @@ -237,3 +256,5 @@ describe('AliasAccessor', () => {
});
});
});

const getAliasFileLocation = (): string => join(tmpdir(), Global.SFDX_STATE_FOLDER, FILENAME);

0 comments on commit a6ff169

Please sign in to comment.