Skip to content

Commit 75482ee

Browse files
authored
test: refactor npm pack test (google#926)
* test: refactor npm pack test * test: refactor copyright test
1 parent 2f2b709 commit 75482ee

File tree

3 files changed

+83
-27
lines changed

3 files changed

+83
-27
lines changed

test/extra.test.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16-
import fs from 'node:fs/promises'
1716
import { test, describe } from 'node:test'
18-
import { globby } from '../build/index.js'
17+
import { globby, fs, path } from '../build/index.js'
18+
19+
const __dirname = path.dirname(new URL(import.meta.url).pathname)
1920

2021
describe('extra', () => {
2122
test('every file should have a license', async () => {
23+
const copyright = await fs.readFile(
24+
path.resolve(__dirname, 'fixtures/copyright.txt'),
25+
'utf8'
26+
)
2227
const files = await globby(['**/*.{js,mjs,ts}', '!**/*polyfill.js'], {
2328
gitignore: true,
2429
onlyFiles: true,
@@ -28,23 +33,7 @@ describe('extra', () => {
2833
for (const file of files) {
2934
const content = await fs.readFile(file, 'utf8')
3035
assert(
31-
content
32-
.replace(/\d{4}/g, 'YEAR')
33-
.includes(
34-
'// Copyright YEAR Google LLC\n' +
35-
'//\n' +
36-
'// Licensed under the Apache License, Version 2.0 (the "License");\n' +
37-
'// you may not use this file except in compliance with the License.\n' +
38-
'// You may obtain a copy of the License at\n' +
39-
'//\n' +
40-
'// https://www.apache.org/licenses/LICENSE-2.0\n' +
41-
'//\n' +
42-
'// Unless required by applicable law or agreed to in writing, software\n' +
43-
'// distributed under the License is distributed on an "AS IS" BASIS,\n' +
44-
'// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' +
45-
'// See the License for the specific language governing permissions and\n' +
46-
'// limitations under the License.'
47-
),
36+
content.replace(/\d{4}/g, 'YEAR').includes(copyright),
4837
`No license header in ${file}.`
4938
)
5039
}

test/fixtures/copyright.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright YEAR Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.

test/package.test.js

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,77 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16-
import { test, describe, beforeEach, before, after } from 'node:test'
16+
import { test, describe, after, before } from 'node:test'
1717
import '../build/globals.js'
1818

19+
const __dirname = new URL('.', import.meta.url).pathname
20+
const root = path.resolve(__dirname, '..')
21+
1922
describe('package', () => {
20-
before(() => syncProcessCwd())
21-
after(() => syncProcessCwd(false))
22-
beforeEach(async () => {
23+
before(async () => {
2324
const pack = await $`npm pack`
2425
await $`tar xf ${pack}`
2526
await $`rm ${pack}`.nothrow()
2627
})
28+
after(async () => {
29+
await $`rm -rf package`
30+
})
31+
32+
test('content looks fine', async () => {
33+
const files = await glob('**/*', {
34+
cwd: path.resolve(root, 'package'),
35+
absolute: false,
36+
onlyFiles: true,
37+
})
38+
assert.deepEqual(
39+
files.sort(),
40+
[
41+
'LICENSE',
42+
'README.md',
43+
'package.json',
44+
'man/zx.1',
45+
'build/cli.cjs',
46+
'build/cli.d.ts',
47+
'build/cli.js',
48+
'build/core.cjs',
49+
'build/core.d.ts',
50+
'build/core.js',
51+
'build/deno.js',
52+
'build/deps.cjs',
53+
'build/deps.d.ts',
54+
'build/deps.js',
55+
'build/esblib.cjs',
56+
'build/globals.cjs',
57+
'build/globals.d.ts',
58+
'build/globals.js',
59+
'build/goods.cjs',
60+
'build/goods.d.ts',
61+
'build/goods.js',
62+
'build/index.cjs',
63+
'build/index.d.ts',
64+
'build/index.js',
65+
'build/repl.cjs',
66+
'build/repl.d.ts',
67+
'build/repl.js',
68+
'build/util.cjs',
69+
'build/util.d.ts',
70+
'build/util.js',
71+
'build/vendor-core.cjs',
72+
'build/vendor-core.d.ts',
73+
'build/vendor-core.js',
74+
'build/vendor-extra.cjs',
75+
'build/vendor-extra.d.ts',
76+
'build/vendor-extra.js',
77+
'build/vendor.cjs',
78+
'build/vendor.d.ts',
79+
'build/vendor.js',
80+
].sort()
81+
)
82+
})
2783

2884
test('ts project', async () => {
29-
const pack = path.resolve('package')
3085
const out = await within(async () => {
31-
cd('test/fixtures/ts-project')
86+
$.cwd = path.resolve(__dirname, 'fixtures/ts-project')
3287
await $`npm i --no-package-lock`
3388
try {
3489
await $`npx tsc`
@@ -41,9 +96,8 @@ describe('package', () => {
4196
})
4297

4398
test('js project with zx', async () => {
44-
const pack = path.resolve('package')
4599
const out = await within(async () => {
46-
cd('test/fixtures/js-project')
100+
$.cwd = path.resolve(__dirname, 'fixtures/js-project')
47101
await $`npm i --no-package-lock`
48102
return $`node node_modules/zx/build/cli.js --verbose script.js`
49103
})

0 commit comments

Comments
 (0)