Commit 75482ee

Anton Golub <antongolub@antongolub.com>
2024-10-30 20:26:15
test: refactor npm pack test (#926)
* test: refactor npm pack test * test: refactor copyright test
1 parent 2f2b709
test/fixtures/copyright.txt
@@ -0,0 +1,13 @@
+// Copyright YEAR Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
test/extra.test.js
@@ -13,12 +13,17 @@
 // limitations under the License.
 
 import assert from 'node:assert'
-import fs from 'node:fs/promises'
 import { test, describe } from 'node:test'
-import { globby } from '../build/index.js'
+import { globby, fs, path } from '../build/index.js'
+
+const __dirname = path.dirname(new URL(import.meta.url).pathname)
 
 describe('extra', () => {
   test('every file should have a license', async () => {
+    const copyright = await fs.readFile(
+      path.resolve(__dirname, 'fixtures/copyright.txt'),
+      'utf8'
+    )
     const files = await globby(['**/*.{js,mjs,ts}', '!**/*polyfill.js'], {
       gitignore: true,
       onlyFiles: true,
@@ -28,23 +33,7 @@ describe('extra', () => {
     for (const file of files) {
       const content = await fs.readFile(file, 'utf8')
       assert(
-        content
-          .replace(/\d{4}/g, 'YEAR')
-          .includes(
-            '// Copyright YEAR Google LLC\n' +
-              '//\n' +
-              '// Licensed under the Apache License, Version 2.0 (the "License");\n' +
-              '// you may not use this file except in compliance with the License.\n' +
-              '// You may obtain a copy of the License at\n' +
-              '//\n' +
-              '//     https://www.apache.org/licenses/LICENSE-2.0\n' +
-              '//\n' +
-              '// Unless required by applicable law or agreed to in writing, software\n' +
-              '// distributed under the License is distributed on an "AS IS" BASIS,\n' +
-              '// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' +
-              '// See the License for the specific language governing permissions and\n' +
-              '// limitations under the License.'
-          ),
+        content.replace(/\d{4}/g, 'YEAR').includes(copyright),
         `No license header in ${file}.`
       )
     }
test/package.test.js
@@ -13,22 +13,77 @@
 // limitations under the License.
 
 import assert from 'node:assert'
-import { test, describe, beforeEach, before, after } from 'node:test'
+import { test, describe, after, before } from 'node:test'
 import '../build/globals.js'
 
+const __dirname = new URL('.', import.meta.url).pathname
+const root = path.resolve(__dirname, '..')
+
 describe('package', () => {
-  before(() => syncProcessCwd())
-  after(() => syncProcessCwd(false))
-  beforeEach(async () => {
+  before(async () => {
     const pack = await $`npm pack`
     await $`tar xf ${pack}`
     await $`rm ${pack}`.nothrow()
   })
+  after(async () => {
+    await $`rm -rf package`
+  })
+
+  test('content looks fine', async () => {
+    const files = await glob('**/*', {
+      cwd: path.resolve(root, 'package'),
+      absolute: false,
+      onlyFiles: true,
+    })
+    assert.deepEqual(
+      files.sort(),
+      [
+        'LICENSE',
+        'README.md',
+        'package.json',
+        'man/zx.1',
+        'build/cli.cjs',
+        'build/cli.d.ts',
+        'build/cli.js',
+        'build/core.cjs',
+        'build/core.d.ts',
+        'build/core.js',
+        'build/deno.js',
+        'build/deps.cjs',
+        'build/deps.d.ts',
+        'build/deps.js',
+        'build/esblib.cjs',
+        'build/globals.cjs',
+        'build/globals.d.ts',
+        'build/globals.js',
+        'build/goods.cjs',
+        'build/goods.d.ts',
+        'build/goods.js',
+        'build/index.cjs',
+        'build/index.d.ts',
+        'build/index.js',
+        'build/repl.cjs',
+        'build/repl.d.ts',
+        'build/repl.js',
+        'build/util.cjs',
+        'build/util.d.ts',
+        'build/util.js',
+        'build/vendor-core.cjs',
+        'build/vendor-core.d.ts',
+        'build/vendor-core.js',
+        'build/vendor-extra.cjs',
+        'build/vendor-extra.d.ts',
+        'build/vendor-extra.js',
+        'build/vendor.cjs',
+        'build/vendor.d.ts',
+        'build/vendor.js',
+      ].sort()
+    )
+  })
 
   test('ts project', async () => {
-    const pack = path.resolve('package')
     const out = await within(async () => {
-      cd('test/fixtures/ts-project')
+      $.cwd = path.resolve(__dirname, 'fixtures/ts-project')
       await $`npm i --no-package-lock`
       try {
         await $`npx tsc`
@@ -41,9 +96,8 @@ describe('package', () => {
   })
 
   test('js project with zx', async () => {
-    const pack = path.resolve('package')
     const out = await within(async () => {
-      cd('test/fixtures/js-project')
+      $.cwd = path.resolve(__dirname, 'fixtures/js-project')
       await $`npm i --no-package-lock`
       return $`node node_modules/zx/build/cli.js --verbose script.js`
     })