generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: switch from axios to got, added tests
- Loading branch information
1 parent
d258d91
commit ef74275
Showing
9 changed files
with
228 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright (c) 2020, salesforce.com, inc. | ||
* All rights reserved. | ||
* 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 | ||
*/ | ||
|
||
export const PLUGIN_INFO_GET_TIMEOUT = (process.env.PLUGIN_INFO_GET_TIMEOUT || 3000) as number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* 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 * as pathPkg from 'path'; | ||
import { expect, use as chaiUse } from 'chai'; | ||
import * as Sinon from 'sinon'; | ||
import * as SinonChai from 'sinon-chai'; | ||
import { stubMethod, spyMethod } from '@salesforce/ts-sinon'; | ||
import * as fsExtra from 'fs-extra'; | ||
import { getString } from '@salesforce/ts-types'; | ||
import { getInfoConfig, PjsonWithInfo } from '../../src/shared/get-info-config'; | ||
|
||
chaiUse(SinonChai); | ||
|
||
describe('getInfoConfig tests', () => { | ||
let sandbox: sinon.SinonSandbox; | ||
let readJsonStub: Sinon.SinonStub; | ||
let joinSpy: Sinon.SinonSpy; | ||
|
||
let pjsonMock: PjsonWithInfo; | ||
|
||
const path = 'path/to'; | ||
|
||
beforeEach(() => { | ||
pjsonMock = { | ||
name: 'testing', | ||
version: '1.2.3', | ||
oclif: { | ||
info: { | ||
releasenotes: { | ||
distTagUrl: 'https://registry.npmjs.org/-/package/sfdx-cli/dist-tags', | ||
releaseNotesPath: 'https://raw.githubusercontent.com/forcedotcom/cli/main/releasenotes/sfdx', | ||
releaseNotesFilename: 'README.md', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
sandbox = Sinon.createSandbox(); | ||
readJsonStub = stubMethod(sandbox, fsExtra, 'readJson').returns(pjsonMock); | ||
joinSpy = spyMethod(sandbox, pathPkg, 'join'); | ||
}); | ||
|
||
afterEach(() => { | ||
joinSpy.restore(); | ||
readJsonStub.restore(); | ||
sandbox.restore(); | ||
}); | ||
|
||
it('join is called with path arg and package.json', async () => { | ||
await getInfoConfig(path); | ||
|
||
expect(joinSpy.args[0]).to.deep.equal([path, 'package.json']); | ||
expect(joinSpy.returnValues[0]).to.equal(`${path}/package.json`); | ||
}); | ||
|
||
it('calls readJson with pjson path', async () => { | ||
await getInfoConfig(path); | ||
|
||
expect(readJsonStub.args[0][0]).to.deep.equal(`${path}/package.json`); | ||
}); | ||
|
||
it('info config is extracted from package.json', async () => { | ||
const info = await getInfoConfig(path); | ||
|
||
expect(info).to.deep.equal(pjsonMock.oclif.info); | ||
}); | ||
|
||
it('throws an error if info config does not exist', async () => { | ||
readJsonStub.returns({ oclif: {} }); | ||
|
||
try { | ||
await getInfoConfig(path); | ||
} catch (err) { | ||
const msg = getString(err, 'message'); | ||
|
||
expect(msg).to.equal('getInfoConfig() failed to find pjson.oclif.info config'); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2018, salesforce.com, inc. | ||
* All rights reserved. | ||
* 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 got from 'got'; | ||
import { expect, use as chaiUse } from 'chai'; | ||
import * as Sinon from 'sinon'; | ||
import * as SinonChai from 'sinon-chai'; | ||
import * as semver from 'semver'; | ||
import { stubMethod, spyMethod } from '@salesforce/ts-sinon'; | ||
import { getReleaseNotes } from '../../src/shared/get-release-notes'; | ||
import { PLUGIN_INFO_GET_TIMEOUT } from '../../src/constants'; | ||
|
||
chaiUse(SinonChai); | ||
|
||
type gotResponse = { | ||
statusCode: number; | ||
body: string; | ||
}; | ||
|
||
describe('getReleaseNotes tests', () => { | ||
let sandbox: sinon.SinonSandbox; | ||
let gotStub: sinon.SinonStub; | ||
let semverSpy: Sinon.SinonSpy; | ||
|
||
let path: string; | ||
let version: string; | ||
let filename: string; | ||
let options; | ||
let versionedResponse: gotResponse; | ||
let readmeResponse: gotResponse; | ||
|
||
beforeEach(() => { | ||
path = 'https://example.com'; | ||
version = '1.2.3'; | ||
filename = 'readme.md'; | ||
options = { | ||
timeout: PLUGIN_INFO_GET_TIMEOUT, | ||
throwHttpErrors: false, | ||
}; | ||
versionedResponse = { | ||
statusCode: 200, | ||
body: 'versioned response body', | ||
}; | ||
readmeResponse = { | ||
statusCode: 200, | ||
body: 'readme response body', | ||
}; | ||
|
||
sandbox = Sinon.createSandbox(); | ||
gotStub = stubMethod(sandbox, got, 'default'); | ||
semverSpy = spyMethod(sandbox, semver, 'major'); | ||
|
||
gotStub.onCall(0).returns(versionedResponse); | ||
gotStub.onCall(1).returns(readmeResponse); | ||
}); | ||
|
||
afterEach(() => { | ||
semverSpy.restore(); | ||
gotStub.restore(); | ||
sandbox.restore(); | ||
}); | ||
|
||
it('semver.major is called passed version', async () => { | ||
await getReleaseNotes(path, filename, version); | ||
|
||
expect(semverSpy.args[0][0]).to.equal(version); | ||
expect(semverSpy.returnValues[0]).to.equal(1); | ||
}); | ||
|
||
it('makes versioned GET request with correct args', async () => { | ||
await getReleaseNotes(path, filename, version); | ||
|
||
const expected = [`${path}/v1.md`, options]; | ||
|
||
expect(gotStub.args[0]).to.deep.equal(expected); | ||
}); | ||
|
||
it('makes readme GET request with correct args', async () => { | ||
await getReleaseNotes(path, filename, version); | ||
|
||
const expected = [`${path}/${filename}`, { ...options, throwHttpErrors: true }]; | ||
|
||
expect(gotStub.args[1]).to.deep.equal(expected); | ||
}); | ||
|
||
it('returns versioned markdown if found', async () => { | ||
const body = await getReleaseNotes(path, filename, version); | ||
|
||
expect(body).to.equal('versioned response body'); | ||
}); | ||
|
||
it('returns readme markdown if versioned markdown is not found', async () => { | ||
versionedResponse.statusCode = 404; | ||
|
||
const body = await getReleaseNotes(path, filename, version); | ||
|
||
expect(body).to.equal('readme response body'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters