Skip to content

Commit 6cd5a3b

Browse files
authoredAug 10, 2020
fix(client): check in bundled client code into version control (#3524)
New script will take care about building assets and also checking that bundled assets are up-to-date as part of CI. This provides some improvements over the previous approach from f5521df#commitcomment-38967493: 1. It is possible to install `karma` from GitHub branch without the assumption that `browserify` is installed in user's project. 1. Karma contributors no longer need to run `npm run build` unless they want to change client code. 1. Simplifies runtime code.
1 parent 5db46b7 commit 6cd5a3b

12 files changed

+5558
-94
lines changed
 

‎.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
node_modules
22
npm-debug.log
3-
static/context.js
4-
static/karma.js
53
.idea/*
64
*.iml
75
docs/_build

‎.travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ jobs:
77
- node_js: "10"
88
script:
99
- npm run init
10-
- npm run build
1110
- npm run test:unit
1211
- npm run test:e2e
1312

1413
- node_js: "12"
1514
script:
1615
- npm run init
17-
- npm run build
1816
- npm run test:unit
1917
- npm run test:e2e
2018

@@ -23,7 +21,7 @@ jobs:
2321
- npm run init
2422
- commitlint-travis
2523
- npm run lint
26-
- npm run build
24+
- npm run build:check
2725
- npm run test:unit
2826
- npm run test:e2e
2927
- npm run test:client

‎appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ install:
2424
- npm run init:windows
2525

2626
test_script:
27-
- npm run appveyor
27+
- npm run test:appveyor
2828

2929
build: off
3030

‎docs/dev/02-making-changes.md

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ Here are some tips on how to set up a Karma workspace and how to send a good pul
2727
$ npm run init
2828
# or if you're on Windows
2929
$ npm run init:windows
30-
31-
$ npm run build
3230
```
3331

3432
## Testing and Building

‎lib/server.js

-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const tmp = require('tmp')
88
const fs = require('fs')
99
const path = require('path')
1010

11-
const BundleUtils = require('./utils/bundle-utils')
1211
const NetUtils = require('./utils/net-utils')
1312
const root = global || window || this
1413

@@ -116,10 +115,6 @@ class Server extends KarmaEventEmitter {
116115
async start () {
117116
const config = this.get('config')
118117
try {
119-
await Promise.all([
120-
BundleUtils.bundleResourceIfNotExist('client/main.js', 'static/karma.js'),
121-
BundleUtils.bundleResourceIfNotExist('context/main.js', 'static/context.js')
122-
])
123118
this._boundServer = await NetUtils.bindAvailablePort(config.port, config.listenAddress)
124119
this._boundServer.on('connection', (socket) => {
125120
// Attach an error handler to avoid UncaughtException errors.

‎lib/utils/bundle-utils.js

-26
This file was deleted.

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,14 @@
491491
"test:e2e": "cucumber-js test/e2e/*.feature",
492492
"test:client": "grunt test:client",
493493
"test": "npm run test:unit && npm run test:e2e && npm run test:client",
494-
"build": "grunt build",
494+
"build": "node scripts/client.js build",
495+
"build:check": "node scripts/client.js check",
495496
"test:appveyor": "grunt test-appveyor",
496497
"test:integration": "./scripts/integration-tests.sh",
497498
"link": "node --eval \"path=require('path'); require('fs').symlinkSync(path.resolve(__dirname), path.resolve(__dirname, 'node_modules', 'karma'), 'junction')\"",
498499
"unlink": "node --eval \"require('fs').unlinkSync(require('path').resolve(__dirname, 'node_modules', 'karma'))\"",
499500
"init": "rm -rf node_modules/karma && cd node_modules && ln -nsf ../ karma && cd ../",
500501
"init:windows": "(IF EXIST node_modules\\karma (rmdir node_modules\\karma /S /q)) && npm run link",
501-
"appveyor": "npm run build && npm run test:appveyor",
502502
"semantic-release": "semantic-release"
503503
}
504504
}

‎scripts/client.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const browserify = require('browserify')
2+
const fs = require('fs')
3+
const { readFile } = require('fs').promises
4+
5+
const bundleResourceToFile = (inPath, outPath) => {
6+
return new Promise((resolve, reject) => {
7+
browserify(inPath).bundle()
8+
.pipe(fs.createWriteStream(outPath))
9+
.once('finish', () => resolve())
10+
.once('error', (e) => reject(e))
11+
})
12+
}
13+
14+
const bundleResource = (inPath) => {
15+
return new Promise((resolve, reject) => {
16+
browserify(inPath).bundle((err, buffer) => {
17+
if (err != null) {
18+
reject(err)
19+
return
20+
}
21+
22+
resolve(buffer)
23+
})
24+
})
25+
}
26+
27+
const main = async () => {
28+
if (process.argv[2] === 'build') {
29+
await bundleResourceToFile('client/main.js', 'static/karma.js')
30+
await bundleResourceToFile('context/main.js', 'static/context.js')
31+
} else if (process.argv[2] === 'check') {
32+
const expectedClient = await bundleResource('client/main.js')
33+
const expectedContext = await bundleResource('context/main.js')
34+
35+
const actualClient = await readFile('static/karma.js')
36+
const actualContext = await readFile('static/context.js')
37+
38+
if (Buffer.compare(expectedClient, actualClient) !== 0 || Buffer.compare(expectedContext, actualContext) !== 0) {
39+
// eslint-disable-next-line no-throw-literal
40+
throw 'Bundled client assets are outdated. Forgot to run "npm run build"?'
41+
}
42+
} else {
43+
// eslint-disable-next-line no-throw-literal
44+
throw `Unknown command: ${process.argv[2]}`
45+
}
46+
}
47+
48+
main().catch((err) => {
49+
console.error(err)
50+
process.exit(1)
51+
})

‎static/context.js

+2,645
Large diffs are not rendered by default.

‎static/karma.js

+2,858
Large diffs are not rendered by default.

‎test/unit/server.spec.js

-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const Server = require('../../lib/server')
2-
const BundleUtils = require('../../lib/utils/bundle-utils')
32
const NetUtils = require('../../lib/utils/net-utils')
43
const BrowserCollection = require('../../lib/browser_collection')
54
const Browser = require('../../lib/browser')
@@ -132,22 +131,13 @@ describe('server', () => {
132131
let config
133132
beforeEach(() => {
134133
config = { port: 9876, listenAddress: '127.0.0.1' }
135-
sinon.spy(BundleUtils, 'bundleResourceIfNotExist')
136134
sinon.stub(NetUtils, 'bindAvailablePort').resolves(mockBoundServer)
137135
sinon.stub(mockBoundServer, 'address').returns({ port: 9877 })
138136
sinon
139137
.stub(server, 'get')
140138
.withArgs('config').returns(config)
141139
})
142140

143-
it('should compile static resources', (done) => {
144-
server.start().then(() => {
145-
expect(BundleUtils.bundleResourceIfNotExist).to.have.been.calledWith('client/main.js', 'static/karma.js')
146-
expect(BundleUtils.bundleResourceIfNotExist).to.have.been.calledWith('context/main.js', 'static/context.js')
147-
done()
148-
})
149-
})
150-
151141
it('should search for available port', (done) => {
152142
server.start().then(() => {
153143
expect(NetUtils.bindAvailablePort).to.have.been.calledWith(9876, '127.0.0.1')
@@ -171,7 +161,6 @@ describe('server', () => {
171161
var config
172162
beforeEach(() => {
173163
config = { port: 9876, listenAddress: '127.0.0.1', singleRun: false }
174-
sinon.spy(BundleUtils, 'bundleResourceIfNotExist')
175164
sinon.stub(NetUtils, 'bindAvailablePort').resolves(mockBoundServer)
176165
sinon.stub(mockBoundServer, 'address').returns({ port: 9877 })
177166
sinon

‎test/unit/utils/bundle-utils.spec.js

-42
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.