Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor codebase to async/await - util constants #5342

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils/aadGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const aadGroup = {
* Retrieve a single group.
* @param id Group ID.
*/
getGroupById(id: string): Promise<Group> {
async getGroupById(id: string): Promise<Group> {
const requestOptions: CliRequestOptions = {
url: `${graphResource}/v1.0/groups/${id}`,
headers: {
Expand All @@ -26,7 +26,7 @@ export const aadGroup = {
* Get a list of groups by display name.
* @param displayName Group display name.
*/
getGroupsByDisplayName(displayName: string): Promise<Group[]> {
async getGroupsByDisplayName(displayName: string): Promise<Group[]> {
return odata.getAllItems<Group>(`${graphResource}/v1.0/groups?$filter=displayName eq '${formatting.encodeQueryParameter(displayName)}'`);
},

Expand Down
31 changes: 12 additions & 19 deletions src/utils/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ describe('utils/cache', () => {
});

it(`doesn't fail when writing value to cache file fails`, (done) => {
sinon.stub(fs, 'mkdirSync').callsFake(() => undefined);
sinon
.stub(fs, 'writeFile')
.callsFake(() => {
done();
})
.callsArgWith(2, 'error');
sinon.stub(fs, 'mkdirSync').returns(undefined);
sinon.stub(fs, 'writeFile').returns(done()).callsArgWith(2, 'error');
try {
cache.setValue('key', 'value');
}
Expand All @@ -80,10 +75,8 @@ describe('utils/cache', () => {
});

it(`writes value to cache in a cache file`, (done) => {
sinon.stub(fs, 'mkdirSync').callsFake(() => undefined);
sinon.stub(fs, 'writeFile').callsFake(() => {
done();
});
sinon.stub(fs, 'mkdirSync').returns(undefined);
sinon.stub(fs, 'writeFile').returns(done());
try {
cache.setValue('key', 'value');
}
Expand Down Expand Up @@ -129,12 +122,12 @@ describe('utils/cache', () => {
.onSecondCall()
.callsArgWith(1, undefined, { isDirectory: () => false, atime: twoDaysAgo });
const unlinkStub = sinon.stub(fs, 'unlink')
.callsFake(() => { })
.returns()
.callsArg(1);
try {
cache.clearExpired(() => {
try {
assert(unlinkStub.calledWith(path.join(cache.cacheFolderPath, 'file')));
assert(unlinkStub.calledWith(path.join(cache.cacheFolderPath, 'file')));
done();
}
catch (ex) {
Expand All @@ -156,7 +149,7 @@ describe('utils/cache', () => {
.onFirstCall()
.callsArgWith(1, undefined, { isDirectory: () => true });
const unlinkStub = sinon.stub(fs, 'unlink')
.callsFake(() => { })
.returns()
.callsArg(1);
try {
cache.clearExpired(() => {
Expand Down Expand Up @@ -185,7 +178,7 @@ describe('utils/cache', () => {
.onSecondCall()
.callsArgWith(1, undefined, { isDirectory: () => false, atime: twoDaysAgo });
const unlinkStub = sinon.stub(fs, 'unlink')
.callsFake(() => { })
.returns()
.callsArg(1);
try {
cache.clearExpired(() => {
Expand All @@ -211,7 +204,7 @@ describe('utils/cache', () => {
.stub(fs, 'stat')
.callsArgWith(1, undefined, { isDirectory: () => false, atime: twoDaysAgo });
const unlinkStub = sinon.stub(fs, 'unlink')
.callsFake(() => { })
.returns()
.callsArgWith(1, 'error');
try {
cache.clearExpired(() => {
Expand Down Expand Up @@ -240,7 +233,7 @@ describe('utils/cache', () => {
.onSecondCall()
.callsArgWith(1, undefined, { isDirectory: () => false, atime: twoDaysAgo });
const unlinkStub = sinon.stub(fs, 'unlink')
.callsFake(() => { })
.returns()
.callsArg(1);
try {
cache.clearExpired(() => {
Expand Down Expand Up @@ -269,7 +262,7 @@ describe('utils/cache', () => {
.onSecondCall()
.callsArgWith(1, undefined, { isDirectory: () => false, atime: new Date() });
const unlinkStub = sinon.stub(fs, 'unlink')
.callsFake(() => { })
.returns()
.callsArg(1);
try {
cache.clearExpired(() => {
Expand Down Expand Up @@ -298,7 +291,7 @@ describe('utils/cache', () => {
.onSecondCall()
.callsArgWith(1, undefined, { isDirectory: () => false, atime: twoDaysAgo });
const unlinkStub = sinon.stub(fs, 'unlink')
.callsFake(() => { })
.returns()
.callsArg(1);
try {
cache.clearExpired(() => {
Expand Down
60 changes: 20 additions & 40 deletions src/utils/pid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('utils/pid', () => {
let cacheSetValueStub: sinon.SinonStub;

before(() => {
sinon.stub(cache, 'getValue').callsFake(() => undefined);
cacheSetValueStub = sinon.stub(cache, 'setValue').callsFake(() => undefined);
sinon.stub(cache, 'getValue').returns(undefined);
cacheSetValueStub = sinon.stub(cache, 'setValue').returns(undefined);
});

afterEach(() => {
Expand All @@ -29,72 +29,56 @@ describe('utils/pid', () => {
});

it('retrieves process name on Windows', () => {
sinon.stub(os, 'platform').callsFake(() => 'win32');
sinon.stub(child_process, 'spawnSync').callsFake(() => {
return {
stdout: 'pwsh'
} as any;
});
sinon.stub(os, 'platform').returns('win32');
sinon.stub(child_process, 'spawnSync').returns({ stdout: 'pwsh' } as any);

assert.strictEqual(pid.getProcessName(123), 'pwsh');
});

it('retrieves process name on macOS', () => {
sinon.stub(os, 'platform').callsFake(() => 'darwin');
sinon.stub(child_process, 'spawnSync').callsFake(() => {
return {
stdout: '/bin/bash'
} as any;
});
sinon.stub(os, 'platform').returns('darwin');
sinon.stub(child_process, 'spawnSync').returns({ stdout: '/bin/bash' } as any);

assert.strictEqual(pid.getProcessName(123), '/bin/bash');
});

it('retrieves undefined on macOS when retrieving the process name failed', () => {
sinon.stub(os, 'platform').callsFake(() => 'darwin');
sinon.stub(child_process, 'spawnSync').callsFake(() => {
return {
error: 'An error has occurred'
} as any;
});
sinon.stub(os, 'platform').returns('darwin');
sinon.stub(child_process, 'spawnSync').returns({ error: 'An error has occurred' } as any);

assert.strictEqual(pid.getProcessName(123), undefined);
});

it('retrieves process name on Linux', () => {
sinon.stub(os, 'platform').callsFake(() => 'linux');
sinon.stub(fs, 'existsSync').callsFake(() => true);
sinon.stub(fs, 'readFileSync').callsFake(() => '(pwsh)');
sinon.stub(os, 'platform').returns('linux');
sinon.stub(fs, 'existsSync').returns(true);
sinon.stub(fs, 'readFileSync').returns('(pwsh)');

assert.strictEqual(pid.getProcessName(123), 'pwsh');
});

it(`returns undefined on Linux if the process is not found`, () => {
sinon.stub(os, 'platform').callsFake(() => 'linux');
sinon.stub(fs, 'existsSync').callsFake(() => false);
sinon.stub(os, 'platform').returns('linux');
sinon.stub(fs, 'existsSync').returns(false);

assert.strictEqual(pid.getProcessName(123), undefined);
});

it('returns undefined name on other platforms', () => {
sinon.stub(os, 'platform').callsFake(() => 'android');
sinon.stub(os, 'platform').returns('android');

assert.strictEqual(pid.getProcessName(123), undefined);
});

it('returns undefined when retrieving process name on Windows fails', () => {
sinon.stub(os, 'platform').callsFake(() => 'win32');
sinon.stub(child_process, 'spawnSync').callsFake(() => {
return {
error: 'An error has occurred'
} as any;
});
sinon.stub(os, 'platform').returns('win32');
sinon.stub(child_process, 'spawnSync').returns({ error: 'An error has occurred' } as any);

assert.strictEqual(pid.getProcessName(123), undefined);
});

it('returns undefined when extracting process name on Windows', () => {
sinon.stub(os, 'platform').callsFake(() => 'win32');
sinon.stub(os, 'platform').returns('win32');
sinon.stub(child_process, 'spawnSync').callsFake(command => {
if (command === 'wmic') {
return {
Expand All @@ -113,12 +97,8 @@ pwsh.exe\
});

it('stores retrieved process name in cache', () => {
sinon.stub(os, 'platform').callsFake(() => 'win32');
sinon.stub(child_process, 'spawnSync').callsFake(() => {
return {
stdout: 'pwsh'
} as any;
});
sinon.stub(os, 'platform').returns('win32');
sinon.stub(child_process, 'spawnSync').returns({ stdout: 'pwsh' } as any);

pid.getProcessName(123);

Expand All @@ -127,7 +107,7 @@ pwsh.exe\

it('retrieves process name from cache when available', () => {
sinonUtil.restore(cache.getValue);
sinon.stub(cache, 'getValue').callsFake(() => 'pwsh');
sinon.stub(cache, 'getValue').returns('pwsh');
const osPlatformSpy = sinon.spy(os, 'platform');

assert.strictEqual(pid.getProcessName(123), 'pwsh');
Expand Down
14 changes: 7 additions & 7 deletions src/utils/powerPlatform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { sinonUtil } from "./sinonUtil";

describe('utils/powerPlatform', () => {
before(() => {
sinon.stub(auth, 'restoreAuth').callsFake(() => Promise.resolve());
sinon.stub(auth, 'restoreAuth').resolves();
auth.service.connected = true;
});

Expand All @@ -25,16 +25,16 @@ describe('utils/powerPlatform', () => {
it('returns correct dynamics url as admin', async () => {
const envResponse: any = { "properties": { "linkedEnvironmentMetadata": { "instanceApiUrl": "https://contoso-dev.api.crm4.dynamics.com" } } };

sinon.stub(request, 'get').callsFake((opts) => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url === `https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments/someRandomGuid?api-version=2020-10-01&$select=properties.linkedEnvironmentMetadata.instanceApiUrl`)) {
if (opts.headers &&
opts.headers.accept &&
(opts.headers.accept as string).indexOf('application/json') === 0) {
return Promise.resolve(envResponse);
return envResponse;
}
}

return Promise.reject('Invalid request');
throw 'Invalid request';
});

const actual = await powerPlatform.getDynamicsInstanceApiUrl('someRandomGuid', true);
Expand All @@ -44,16 +44,16 @@ describe('utils/powerPlatform', () => {
it('returns correct dynamics url', async () => {
const envResponse: any = { "properties": { "linkedEnvironmentMetadata": { "instanceApiUrl": "https://contoso-dev.api.crm4.dynamics.com" } } };

sinon.stub(request, 'get').callsFake((opts) => {
sinon.stub(request, 'get').callsFake(async (opts) => {
if ((opts.url === `https://api.bap.microsoft.com/providers/Microsoft.BusinessAppPlatform/environments/someRandomGuid?api-version=2020-10-01&$select=properties.linkedEnvironmentMetadata.instanceApiUrl`)) {
if (opts.headers &&
opts.headers.accept &&
(opts.headers.accept as string).indexOf('application/json') === 0) {
return Promise.resolve(envResponse);
return envResponse;
}
}

return Promise.reject('Invalid request');
throw 'Invalid request';
});

const actual = await powerPlatform.getDynamicsInstanceApiUrl('someRandomGuid', false);
Expand Down
3 changes: 1 addition & 2 deletions src/utils/powerPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export const powerPlatform = {

try {
const response = await request.get<any>(requestOptions);

return Promise.resolve(response.properties.linkedEnvironmentMetadata.instanceApiUrl);
return response.properties.linkedEnvironmentMetadata.instanceApiUrl;
}
catch (ex: any) {
throw Error(`The environment '${environment}' could not be retrieved. See the inner exception for more details: ${ex.message}`);
Expand Down