Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7b72ec2

Browse files
imcaizhengsharathkumaranbu
authored andcommittedDec 16, 2019
Improving tests
1 parent a83b388 commit 7b72ec2

13 files changed

+93
-98
lines changed
 

‎bin/topcoder-cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ program
187187
.description('Let copilot/managers process private task payments')
188188
.option('-o --copilot <payment>', 'copilot payment.')
189189
.option('--dev', 'Points to Topcoder development environment')
190-
.action(args => {
190+
.action(async args => {
191191
if (args.dev) {
192192
process.env.NODE_ENV = 'dev'
193193
}
194-
payHandler.handleCommand(program.args)
194+
await payHandler.handleCommand(program.args)
195195
})
196196

197197
// error on unknown commands

‎docs/Development.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ The following parameters can be set in config files or in env variables:
1919
| TC_CLIENT_V2CONNECTION | CLIENT_V2CONNECTION | TC-User-Database | TC client connection protocol |
2020
| AUTH0_AUDIENCE | AUTH0_AUDIENCE | https://m2m.topcoder.com/ | AUTH0 Audience (For M2M) |
2121

22-
# Configuration for Test
23-
Configuration for test is at `test/common/testConfig.js`.
24-
The following parameters can be set in config files or in env variables:
25-
26-
| Property | Environment varible | Default value | Description |
27-
| --- | --- | --- | --- |
28-
| WAIT_TIME | WAIT_TIME | 500 | Waiting time for the CLI tool to process subcommands. Increase the value if needed |
29-
3022
# Publish the package to npm
3123
- Create a npm account on https://www.npmjs.com/signup if you don't have one.
3224
- Use the account to sign in via cli: `npm login`

‎package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"homepage": "https://topcoder-platform.github.io/topcoder-cli/",
4444
"devDependencies": {
4545
"chai": "^4.2.0",
46-
"delay": "^4.3.0",
4746
"mocha": "^6.1.4",
4847
"mocha-prepare": "^0.1.0",
4948
"mock-require": "^3.0.3",

‎src/commands/pay.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const logger = require('../common/logger')
55
* Handles the "pay" command
66
* @param {Array} args Arguments
77
*/
8-
function handleCommand (args) {
8+
async function handleCommand (args) {
99
const options = args[0].opts()
1010
const challengeDetails = [
1111
{
@@ -45,7 +45,7 @@ function handleCommand (args) {
4545
logger.info(response)
4646
// => response => { username, age, about }
4747
}
48-
promptQuestions()
48+
await promptQuestions()
4949
}
5050

5151
module.exports = {

‎test/cli.test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/*
22
* Test CLI functionalities.
33
*/
4-
const delay = require('delay')
54
const chai = require('chai')
65
const _ = require('lodash')
76

87
const { program, docs } = require('../bin/topcoder-cli')
98
const testHelper = require('./common/testHelper')
10-
const testConfig = require('./common/testConfig')
119

1210
const localTestData = {
1311
argsWithHelp: testHelper.buildArgs(undefined, { help: true }),
@@ -57,31 +55,26 @@ describe('CLI Test', async function () {
5755

5856
it('It should show general help info', async function () {
5957
program.parse(localTestData.argsWithHelp)
60-
await delay(testConfig.WAIT_TIME)
6158
chai.expect(_.last(messages)).to.include('Topcoder CLI to interact with Topcoder systems')
6259
})
6360

6461
it('It should show help documentation for command submit', async function () {
6562
program.parse(localTestData.argsForCommandSubmit)
66-
await delay(testConfig.WAIT_TIME)
6763
chai.expect(_.last(messages)).to.equal(docs.submit)
6864
})
6965

7066
it('It should show help documentation for command fetch-submissions', async function () {
7167
program.parse(localTestData.argsForCommandFetchSubmissions)
72-
await delay(testConfig.WAIT_TIME)
7368
chai.expect(_.last(messages)).to.equal(docs['fetch-submissions'])
7469
})
7570

7671
it('It should show help documentation for command fetch-artifacts', async function () {
7772
program.parse(localTestData.argsForCommandFetchArtifacts)
78-
await delay(testConfig.WAIT_TIME)
7973
chai.expect(_.last(messages)).to.equal(docs['fetch-artifacts'])
8074
})
8175

8276
it('failure - It should handle unknown commands', async function () {
8377
program.parse(localTestData.argsWithUnknownCommand)
84-
await delay(testConfig.WAIT_TIME)
8578
chai.expect(_.last(errorMessages)).to.include('Invalid command:')
8679
})
8780
})

‎test/common/testConfig.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎test/common/testHelper.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/*
22
* Helper functions for test.
33
*/
4-
const AdmZip = require('adm-zip')
54
const _ = require('lodash')
65
const fs = require('fs-extra')
76
const ini = require('ini')
87
const { ObjectWritableMock } = require('stream-mock')
8+
const EventEmitter = require('events')
9+
const commander = require('commander')
910

10-
function listZipEntries (buffer) {
11-
const zip = new AdmZip(buffer)
12-
return zip.getEntries()
13-
}
11+
const commandEmitter = new EventEmitter()
1412

1513
/**
1614
* Build command line arguments.
@@ -144,12 +142,44 @@ function parseMultipart (text) {
144142
return data
145143
}
146144

145+
/**
146+
* Inject an event emitter into every actions,
147+
* so that we can inpect an action completed by listening to the `action:finish` event.
148+
*
149+
* @returns {undefined}
150+
*/
151+
function spyActions () {
152+
const originalAction = commander.Command.prototype.action
153+
commander.Command.prototype.action = function (action) {
154+
originalAction.call(this, async (...args) => {
155+
try {
156+
await action(...args)
157+
} catch (err) {
158+
// ignore
159+
}
160+
commandEmitter.emit('action:finish')
161+
})
162+
}
163+
}
164+
165+
/**
166+
* Wait for an action finished by listening to the `action:finish` event.
167+
*
168+
* @returns {Promise}
169+
*/
170+
async function waitForCommandExit () {
171+
return new Promise((resolve) => {
172+
commandEmitter.on('action:finish', () => { resolve() })
173+
})
174+
}
175+
147176
module.exports = {
148-
listZipEntries,
149177
buildArgs,
150178
mockFunction,
151179
mockRCConfig,
152180
mockGlobalConfig,
153181
mockDownload,
154-
parseMultipart
182+
parseMultipart,
183+
spyActions,
184+
waitForCommandExit
155185
}

‎test/config.command.test.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
* Test for the Config command.
33
*/
44
const chai = require('chai')
5-
const delay = require('delay')
65
const fs = require('fs-extra')
76
const _ = require('lodash')
87
const ini = require('ini')
98

109
const testHelper = require('./common/testHelper')
1110
const testData = require('./common/testData')
12-
const testConfig = require('./common/testConfig')
1311
const logger = require('../src/common/logger')
1412
let { program } = require('../bin/topcoder-cli')
1513

@@ -67,46 +65,46 @@ describe('Config Command Test', async function () {
6765
}
6866
)
6967
program.parse(localTestData.argsWithList)
70-
await delay(testConfig.WAIT_TIME)
68+
await testHelper.waitForCommandExit()
7169
chai.expect(_.last(messages)).to.equal(localTestData.globalConfigWithUserCredentials)
7270
})
7371

7472
it('success - add global config', async function () {
7573
program.parse(localTestData.argsWithAdd)
76-
await delay(testConfig.WAIT_TIME)
74+
await testHelper.waitForCommandExit()
7775
chai.expect(content[0]).to.include('username=aaron2017')
7876
})
7977

8078
it('success - add global config when config file not found', async function () {
8179
mocks.readFile.restore()
8280
mocks.readFile = testHelper.mockFunction(fs, 'readFile', () => { throw new Error() })
8381
program.parse(localTestData.argsWithAdd)
84-
await delay(testConfig.WAIT_TIME)
82+
await testHelper.waitForCommandExit()
8583
chai.expect(content[0]).to.include('username=aaron2017')
8684
})
8785

8886
it('success - unset global config', async function () {
8987
program.parse(localTestData.argsWithUnset)
90-
await delay(testConfig.WAIT_TIME)
88+
await testHelper.waitForCommandExit()
9189
chai.expect(content[0]).to.include('password=xxx123')
9290
chai.expect(content[0]).to.not.include('username=TonyJ')
9391
})
9492

9593
it('failure - add global config with invalid key', async function () {
9694
program.parse(localTestData.argsWithAddInvalidKey)
97-
await delay(testConfig.WAIT_TIME)
95+
await testHelper.waitForCommandExit()
9896
chai.expect(_.last(errorMessages)).to.include('Invalid key value.')
9997
})
10098

10199
it('failure - add global config with extra values', async function () {
102100
program.parse(localTestData.argsWithExtraValues)
103-
await delay(testConfig.WAIT_TIME)
101+
await testHelper.waitForCommandExit()
104102
chai.expect(_.last(errorMessages)).to.include('Invalid number of values passed.')
105103
})
106104

107105
it('failure - unset global config with non-existent key', async function () {
108106
program.parse(localTestData.argsWithUnsetNotExistentKey)
109-
await delay(testConfig.WAIT_TIME)
107+
await testHelper.waitForCommandExit()
110108
chai.expect(_.last(errorMessages)).to.include(`${invalidConfig[0]} is not found in the config file`)
111109
})
112110
})

‎test/fetchArtifacts.command.test.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
* Test for the FetchArtifacts command.
33
*/
44
const chai = require('chai')
5-
const delay = require('delay')
65
const path = require('path')
76
const _ = require('lodash')
87

98
const logger = require('../src/common/logger')
109
const testHelper = require('./common/testHelper')
1110
const testData = require('./common/testData')
12-
const testConfig = require('./common/testConfig')
1311
let { program } = require('../bin/topcoder-cli')
1412

1513
const submissionId = '4d38573f-404c-4c2f-90b1-a05ccd3fe860'
@@ -83,7 +81,7 @@ describe('FetchArtifacts Command Test', async function () {
8381

8482
it('success - fetch artifacts to local', async function () {
8583
program.parse(localTestData.argsWithSubmissionId)
86-
await delay(testConfig.WAIT_TIME)
84+
await testHelper.waitForCommandExit()
8785
chai.expect(testData._variables.artifactDownloadInfo.length).to.equal(
8886
testData.responses.submissionAPI.searchArtifacts.artifacts.length
8987
)
@@ -98,7 +96,7 @@ describe('FetchArtifacts Command Test', async function () {
9896
mocks.returnEmptyRC.restore()
9997
mocks.mockRCConfig = testHelper.mockRCConfig({ submissionId, ...credentials })
10098
program.parse(localTestData.argsBasic)
101-
await delay(testConfig.WAIT_TIME)
99+
await testHelper.waitForCommandExit()
102100
chai.expect(testData._variables.artifactDownloadInfo.length).to.equal(
103101
testData.responses.submissionAPI.searchArtifacts.artifacts.length
104102
)
@@ -111,7 +109,7 @@ describe('FetchArtifacts Command Test', async function () {
111109
mocks.mockRCConfig = testHelper.mockRCConfig({ submissionId })
112110
mocks.mockGlobalConfig = testHelper.mockGlobalConfig(credentials)
113111
program.parse(localTestData.argsBasic)
114-
await delay(testConfig.WAIT_TIME)
112+
await testHelper.waitForCommandExit()
115113
chai.expect(testData._variables.artifactDownloadInfo.length).to.equal(
116114
testData.responses.submissionAPI.searchArtifacts.artifacts.length
117115
)
@@ -121,7 +119,7 @@ describe('FetchArtifacts Command Test', async function () {
121119

122120
it('success - fetch artifacts to local with argument dev', async function () {
123121
program.parse(localTestData.argsWithDev)
124-
await delay(testConfig.WAIT_TIME)
122+
await testHelper.waitForCommandExit()
125123
chai.expect(process.env.NODE_ENV).to.equal('dev')
126124
chai.expect(testData._variables.artifactDownloadInfo.length).to.equal(
127125
testData.responses.submissionAPI.searchArtifacts.artifacts.length
@@ -131,7 +129,7 @@ describe('FetchArtifacts Command Test', async function () {
131129

132130
it('success - fetch artifacts to local with argument legacySubmissionId', async function () {
133131
program.parse(localTestData.argsWithLegacySubmissionId)
134-
await delay(testConfig.WAIT_TIME)
132+
await testHelper.waitForCommandExit()
135133
chai.expect(testData._variables.artifactDownloadInfo.length).to.equal(
136134
testData.responses.submissionAPI.searchArtifacts.artifacts.length
137135
)
@@ -140,14 +138,14 @@ describe('FetchArtifacts Command Test', async function () {
140138

141139
it('success - show info if no artifacts found', async function () {
142140
program.parse(localTestData.argsWithSubmissionIdContainingZeroArtifacts)
143-
await delay(testConfig.WAIT_TIME)
141+
await testHelper.waitForCommandExit()
144142
chai.expect(_.nth(messages, -2)).to.equal(`No artifact exists for submission with ID: ${testData.submissionIdWithZeroArtifacts}.`)
145143
chai.expect(_.nth(messages, -1)).to.equal('All Done!')
146144
})
147145

148146
it('failure - fetch artifacts missing password', async function () {
149147
program.parse(localTestData.argsWithoutPassword)
150-
await delay(testConfig.WAIT_TIME)
148+
await testHelper.waitForCommandExit()
151149
chai.expect(_.last(errorMessages)).to.include('"username" missing required peer "password"')
152150
})
153151

@@ -157,7 +155,7 @@ describe('FetchArtifacts Command Test', async function () {
157155
mocks.mockRCConfig = testHelper.mockRCConfig({ submissionId })
158156
mocks.mockGlobalConfig = testHelper.mockGlobalConfig(_.pick(testData.m2mConfig, ['m2m.client_secret']))
159157
program.parse(localTestData.argsBasic)
160-
await delay(testConfig.WAIT_TIME)
158+
await testHelper.waitForCommandExit()
161159
chai.expect(_.last(errorMessages)).to.include('m2m.client_id" is required')
162160
})
163161

@@ -167,34 +165,34 @@ describe('FetchArtifacts Command Test', async function () {
167165
mocks.mockRCConfig = testHelper.mockRCConfig({ submissionId })
168166
mocks.mockGlobalConfig = testHelper.mockGlobalConfig(_.pick(testData.m2mConfig, ['m2m.client_id']))
169167
program.parse(localTestData.argsBasic)
170-
await delay(testConfig.WAIT_TIME)
168+
await testHelper.waitForCommandExit()
171169
chai.expect(_.last(errorMessages)).to.include('m2m.client_secret" is required')
172170
})
173171

174172
it('failure - fetch artifacts without submissionId or legacySubmissionId', async function () {
175173
program.parse(localTestData.argsWithoutId)
176-
await delay(testConfig.WAIT_TIME)
174+
await testHelper.waitForCommandExit()
177175
chai.expect(_.last(errorMessages)).to.include('"value" must contain at least one of [submissionId, legacySubmissionId]')
178176
})
179177

180178
it('failure - fetch artifacts provided both submissionId and legacySubmissionId', async function () {
181179
program.parse(localTestData.argsWithBothIds)
182-
await delay(testConfig.WAIT_TIME)
180+
await testHelper.waitForCommandExit()
183181
chai.expect(_.last(errorMessages)).to.include('contains a conflict between exclusive peers [submissionId, legacySubmissionId]')
184182
})
185183

186184
it('failure - it should handle possible request errors', async function () {
187185
testData._variables.needErrorResponse = true // instruct nock server to return 500
188186
program.parse(localTestData.argsWithSubmissionId)
189-
await delay(testConfig.WAIT_TIME)
187+
await testHelper.waitForCommandExit()
190188
chai.expect(_.nth(errorMessages, 0)).to.include('Couldn\'t download artifact')
191189
})
192190

193191
it('failure - fetch artifacts provided both m2m and userCredentials', async function () {
194192
mocks.returnEmptyRC.restore()
195193
mocks.mockRCConfig = testHelper.mockRCConfig({ submissionId, ...testData.m2mConfig })
196194
program.parse(localTestData.argsWithSubmissionId)
197-
await delay(testConfig.WAIT_TIME)
195+
await testHelper.waitForCommandExit()
198196
chai.expect(_.last(errorMessages)).to.include('contains a conflict between exclusive peers [username, m2m]')
199197
})
200198
})

0 commit comments

Comments
 (0)
This repository has been archived.