Skip to content

Commit 8eeb027

Browse files
committed
test: add more tests for installing updates
1 parent bca37d9 commit 8eeb027

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@types/node": "~12.20.0",
5656
"@types/semver": "^7.1.0",
5757
"@types/sinon": "^10.0.0",
58+
"@types/sinon-chai": "^3.2.5",
5859
"@types/tmp": "^0.2.0",
5960
"@typescript-eslint/eslint-plugin": "^4.3.0",
6061
"@typescript-eslint/parser": "^4.3.0",
@@ -77,6 +78,7 @@
7778
"pinst": "^2.1.6",
7879
"semantic-release": "^17.1.1",
7980
"sinon": "^11.1.1",
81+
"sinon-chai": "^3.7.0",
8082
"tmp": "^0.2.0",
8183
"ts-node": "^10.0.0",
8284
"typescript": "^4.0.2"

tests/completions-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describe('completions', () => {
117117

118118
it('Generate Alloy Completions without alloy installed', async () => {
119119
mockFS({ ...mockAppcCli(true), ...mockNpmAlloy(true) });
120-
expect(generateAlloyCompletions(true)).to.be.rejectedWith(Error, 'Unable to find installed alloy version.');
120+
await expect(generateAlloyCompletions(true)).to.be.rejectedWith(Error, 'Unable to find Alloy');
121121
});
122122

123123
it('Generate Alloy Completions with pre-existing completions', async () => {

tests/updates-test.ts

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
import * as titaniumlib from 'titaniumlib';
2+
import * as path from 'path';
3+
import * as util from '../src/util';
4+
15
import execa from 'execa';
26
import { appc, titanium, node, alloy, checkAllUpdates } from '../src/updates/';
3-
import * as util from '../src/util';
47

5-
import { expect } from 'chai';
8+
import chai from 'chai';
9+
import chaiAsPromised from 'chai-as-promised';
610
import mockFS from 'mock-fs';
711
import nock from 'nock';
812
import os from 'os';
9-
import * as path from 'path';
13+
import sinon from 'sinon';
14+
import sinonChai from 'sinon-chai';
15+
1016
import { mockAppcCoreRequest, mockNpmRequest, mockSDKRequest, mockNodeRequest } from './fixtures/network/network-mocks';
1117
import { mockAppcCli, mockNode, mockNpmCli, mockNpmInstall, mockOS, mockSdk } from './util';
1218

19+
chai.use(chaiAsPromised);
20+
chai.use(sinonChai);
21+
const expect = chai.expect;
22+
1323
let fixProcessPlatform: () => void|undefined;
1424
describe('updates', () => {
1525

@@ -57,6 +67,64 @@ describe('updates', () => {
5767
expect(update.hasUpdate).to.equal(false);
5868
});
5969

70+
it('install with titanium cli', async () => {
71+
72+
global.sandbox
73+
.stub(titaniumlib.sdk, 'install')
74+
.resolves('');
75+
76+
const execStub: sinon.SinonStub = global.sandbox.stub(util, 'exec');
77+
78+
const selectStub = execStub
79+
.withArgs('ti', sinon.match.any, sinon.match.any)
80+
.resolves({ stdout: '{}' } as execa.ExecaReturnValue);
81+
82+
mockNpmCli(execStub, 'titanium', '5.3.0');
83+
84+
await titanium.sdk.installUpdate('8.0.0.GA');
85+
expect(selectStub).to.have.been.calledOnceWith('ti', [ 'sdk', 'select', '8.0.0.GA' ], { shell: true });
86+
});
87+
88+
it('install with appc cli logged in', async () => {
89+
global.sandbox
90+
.stub(titaniumlib.sdk, 'install')
91+
.resolves('');
92+
93+
const execStub: sinon.SinonStub = global.sandbox.stub(util, 'exec');
94+
95+
const whoamiStub = execStub
96+
.withArgs('appc', [ 'whoami', '-o', 'json'], sinon.match.any)
97+
.resolves({ stdout: '{ "username": "bob" }' } as execa.ExecaReturnValue);
98+
99+
const selectStub = execStub
100+
.withArgs('appc', [ 'ti', 'sdk', 'select', '8.0.0.GA' ], sinon.match.any)
101+
.resolves({ stdout: '' } as execa.ExecaReturnValue);
102+
103+
mockNpmCli(execStub, 'titanium');
104+
mockAppcCli(execStub, '6.6.6', '4.2.13');
105+
106+
await titanium.sdk.installUpdate('8.0.0.GA');
107+
expect(whoamiStub).to.have.been.calledOnceWith('appc', [ 'whoami', '-o', 'json' ], { shell: true });
108+
expect(selectStub).to.have.been.calledOnceWith('appc', [ 'ti', 'sdk', 'select', '8.0.0.GA' ], { shell: true });
109+
});
110+
111+
it('install with appc cli logged out', async () => {
112+
global.sandbox
113+
.stub(titaniumlib.sdk, 'install')
114+
.resolves('');
115+
116+
const execStub: sinon.SinonStub = global.sandbox.stub(util, 'exec');
117+
118+
execStub
119+
.withArgs('appc', [ 'whoami', '-o', 'json'], sinon.match.any)
120+
.resolves({ stdout: '{}' } as execa.ExecaReturnValue);
121+
122+
mockNpmCli(execStub, 'titanium');
123+
mockAppcCli(execStub, '6.6.6', '4.2.13');
124+
125+
await expect(titanium.sdk.installUpdate('8.0.0.GA')).to.eventually.be.rejectedWith('Failed to select SDK as you are not logged in');
126+
});
127+
60128
it('getReleaseNotes()', () => {
61129
expect(titanium.sdk.getReleaseNotes('10.0.0.GA')).to.equal('https://titaniumsdk.com/guide/Titanium_SDK/Titanium_SDK_Release_Notes/Titanium_SDK_Release_Notes_10.x/Titanium_SDK_10.0.0.GA_Release_Note.html');
62130
});
@@ -121,6 +189,16 @@ describe('updates', () => {
121189
expect(update.hasUpdate).to.equal(false);
122190
});
123191

192+
it('installUpdate()', async () => {
193+
const stub = global.sandbox.stub(util, 'exec')
194+
.withArgs('npm', sinon.match.any, sinon.match.any)
195+
.resolves({ stdout: '' } as execa.ExecaReturnValue);
196+
197+
await appc.install.installUpdate('6.6.6');
198+
199+
expect(stub).to.have.been.calledOnceWith('npm', [ 'install', '-g', '[email protected]', '--json' ], { shell: true });
200+
});
201+
124202
it('getReleaseNotes()', () => {
125203
expect(appc.install.getReleaseNotes()).to.equal('https://titaniumsdk.com/guide/Appcelerator_CLI/Appcelerator_CLI_Release_Notes/');
126204
});
@@ -184,6 +262,16 @@ describe('updates', () => {
184262
expect(update.hasUpdate).to.equal(true);
185263
});
186264

265+
it('installUpdate()', async () => {
266+
const stub = global.sandbox.stub(util, 'exec')
267+
.withArgs('appc', sinon.match.any, sinon.match.any)
268+
.resolves({ stdout: '' } as execa.ExecaReturnValue);
269+
270+
await appc.core.installUpdate('6.6.6');
271+
272+
expect(stub).to.have.been.calledOnceWith('appc', [ 'use', '6.6.6' ], { shell: true });
273+
});
274+
187275
it('getReleaseNotes()', () => {
188276
expect(appc.core.getReleaseNotes('6.6.6')).to.equal('https://titaniumsdk.com/guide/Appcelerator_CLI/Appcelerator_CLI_Release_Notes/Appcelerator_CLI_Release_Notes_6.x/Appcelerator_CLI_6.6.6_GA_Release_Note.html');
189277
});

tests/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import execa from 'execa';
1616
export function mockAppcCli (stub: sinon.SinonStub, coreVersion?: string, installerVersion?: string, mockVersionFile = false): void {
1717
if (coreVersion && installerVersion) {
1818
stub
19-
.withArgs('appc', sinon.match.any, sinon.match.any)
19+
.withArgs('appc', [ '--version', '--output', 'json' ], sinon.match.any)
2020
.resolves({ stdout: `{"NPM":"${installerVersion}","CLI":"${coreVersion}"}` } as execa.ExecaReturnValue);
2121

2222
if (mockVersionFile) {

0 commit comments

Comments
 (0)