|
| 1 | +name: Internal - Tests for "package/discover" action |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + |
| 6 | +permissions: {} |
| 7 | + |
| 8 | +jobs: |
| 9 | + discover: |
| 10 | + name: Package discovery (${{ matrix.fixture.name }}) |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + strategy: |
| 15 | + fail-fast: false |
| 16 | + matrix: |
| 17 | + fixture: |
| 18 | + - name: standalone npm |
| 19 | + root: { private: false } |
| 20 | + expected: ["."] |
| 21 | + - name: npm workspaces and exclusions |
| 22 | + root: |
| 23 | + private: true |
| 24 | + workspaces: [packages/*, "!packages/excluded"] |
| 25 | + expected: [packages/cli, packages/core] |
| 26 | + - name: workspace root is not implicitly packaged |
| 27 | + root: |
| 28 | + private: false |
| 29 | + workspaces: [packages/core] |
| 30 | + expected: [packages/core] |
| 31 | + - name: Yarn workspace object |
| 32 | + manager: yarn |
| 33 | + root: |
| 34 | + workspaces: { packages: [packages/*, "!packages/excluded"] } |
| 35 | + expected: [packages/cli, packages/core] |
| 36 | + - name: pnpm workspaces |
| 37 | + manager: pnpm |
| 38 | + files: |
| 39 | + pnpm-workspace.yaml: | |
| 40 | + packages: |
| 41 | + - 'packages/*' |
| 42 | + - '!packages/excluded' |
| 43 | + expected: [packages/cli, packages/core] |
| 44 | + - name: pnpm 10 workspaces |
| 45 | + manager: pnpm10 |
| 46 | + files: |
| 47 | + pnpm-workspace.yaml: | |
| 48 | + packages: |
| 49 | + - 'packages/*' |
| 50 | + - '!packages/excluded' |
| 51 | + expected: [packages/cli, packages/core] |
| 52 | + - name: standalone pnpm with settings |
| 53 | + manager: pnpm |
| 54 | + root: { private: false } |
| 55 | + files: |
| 56 | + pnpm-workspace.yaml: "packages: []" |
| 57 | + expected: ["."] |
| 58 | + - name: Lerna package patterns take precedence |
| 59 | + root: { workspaces: [packages/excluded] } |
| 60 | + files: |
| 61 | + lerna.json: '{"version":"independent","packages":["packages/*","!packages/excluded"]}' |
| 62 | + expected: [packages/cli, packages/core] |
| 63 | + - name: explicit subset |
| 64 | + root: { workspaces: [packages/*] } |
| 65 | + directories: '["packages/core"]' |
| 66 | + expected: [packages/core] |
| 67 | + - name: explicit root |
| 68 | + root: { private: false, workspaces: [packages/*] } |
| 69 | + directories: '["."]' |
| 70 | + expected: ["."] |
| 71 | + - name: private standalone |
| 72 | + expected: failure |
| 73 | + - name: private workspaces only |
| 74 | + root: { workspaces: [packages/private] } |
| 75 | + expected: failure |
| 76 | + - name: unmatched workspaces |
| 77 | + root: { workspaces: [missing/*] } |
| 78 | + expected: failure |
| 79 | + - name: explicit private package |
| 80 | + directories: '["packages/private"]' |
| 81 | + expected: failure |
| 82 | + - name: empty override |
| 83 | + directories: "[]" |
| 84 | + expected: failure |
| 85 | + - name: invalid override |
| 86 | + directories: '{"directory":"packages/core"}' |
| 87 | + expected: failure |
| 88 | + - name: escaping directory |
| 89 | + directories: '["../outside"]' |
| 90 | + expected: failure |
| 91 | + - name: duplicate normalized directories |
| 92 | + directories: '["packages/core", "./packages/core/"]' |
| 93 | + expected: failure |
| 94 | + - name: symlink outside the project |
| 95 | + directories: '["linked"]' |
| 96 | + expected: failure |
| 97 | + steps: |
| 98 | + - name: Arrange - Checkout |
| 99 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 100 | + with: |
| 101 | + persist-credentials: false |
| 102 | + - name: Arrange - Create uninstalled project fixtures |
| 103 | + id: fixture |
| 104 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 105 | + env: |
| 106 | + FIXTURE: ${{ toJSON(matrix.fixture) }} |
| 107 | + with: |
| 108 | + script: | |
| 109 | + const fs = require('node:fs'); |
| 110 | + const path = require('node:path'); |
| 111 | + const fixture = JSON.parse(process.env.FIXTURE); |
| 112 | + const temporary = fs.mkdtempSync(path.join(process.env.RUNNER_TEMP, 'package-discovery-')); |
| 113 | + const directory = path.join(temporary, 'project'); |
| 114 | + const write = async (file, content) => { |
| 115 | + const target = path.join(directory, file); |
| 116 | + await io.mkdirP(path.dirname(target)); |
| 117 | + fs.writeFileSync(target, typeof content === 'string' ? content : JSON.stringify(content)); |
| 118 | + }; |
| 119 | + const manifest = { name: 'discovery-root', version: '1.0.0', private: true, ...fixture.root, |
| 120 | + scripts: { preinstall: 'exit 91', postinstall: 'exit 91', prepare: 'exit 91' } }; |
| 121 | + if (fixture.manager?.startsWith('pnpm')) { |
| 122 | + const lock = JSON.parse(fs.readFileSync('actions/release/prepare/package-lock.json', 'utf8')); |
| 123 | + manifest.packageManager = `pnpm@${lock.packages[`node_modules/${fixture.manager}`].version}`; |
| 124 | + await write('pnpm-lock.yaml', "lockfileVersion: '9.0'\n"); |
| 125 | + } else if (fixture.manager === 'yarn') { |
| 126 | + await write('yarn.lock', '# yarn lockfile v1\n'); |
| 127 | + } else { |
| 128 | + await write('package-lock.json', { name: manifest.name, version: manifest.version, lockfileVersion: 3, packages: { '': manifest } }); |
| 129 | + } |
| 130 | + await write('package.json', manifest); |
| 131 | + await write('.node-version', fs.readFileSync('.node-version', 'utf8')); |
| 132 | + for (const name of ['core', 'cli', 'private', 'excluded']) { |
| 133 | + await write(`packages/${name}/package.json`, { name: `@example/${name}`, version: '1.0.0', private: name === 'private', scripts: manifest.scripts }); |
| 134 | + } |
| 135 | + await write('examples/unconfigured/package.json', { name: 'unconfigured', version: '1.0.0' }); |
| 136 | + for (const [file, content] of Object.entries(fixture.files || {})) await write(file, content); |
| 137 | + await io.mkdirP(path.join(temporary, 'outside')); |
| 138 | + fs.writeFileSync(path.join(temporary, 'outside/package.json'), JSON.stringify({ name: 'outside', version: '1.0.0' })); |
| 139 | + fs.symlinkSync(path.join(temporary, 'outside'), path.join(directory, 'linked'), 'dir'); |
| 140 | + core.setOutput('directory', directory); |
| 141 | + const lockfile = fixture.manager?.startsWith('pnpm') ? 'pnpm-lock.yaml' : fixture.manager === 'yarn' ? 'yarn.lock' : 'package-lock.json'; |
| 142 | + core.setOutput('lockfile', path.join(directory, lockfile)); |
| 143 | + core.setOutput('lock-content', fs.readFileSync(path.join(directory, lockfile), 'utf8')); |
| 144 | + - name: Act - Discover packages without installing dependencies |
| 145 | + id: discover |
| 146 | + continue-on-error: true |
| 147 | + uses: $/actions/package/discover |
| 148 | + with: |
| 149 | + working-directory: ${{ steps.fixture.outputs.directory }} |
| 150 | + directories: ${{ matrix.fixture.directories || '' }} |
| 151 | + - name: Assert - Selection and dependency installation |
| 152 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 153 | + env: |
| 154 | + EXPECTED: ${{ toJSON(matrix.fixture.expected) }} |
| 155 | + OUTCOME: ${{ steps.discover.outcome }} |
| 156 | + DIRECTORIES: ${{ steps.discover.outputs.directories }} |
| 157 | + PROJECT: ${{ steps.fixture.outputs.directory }} |
| 158 | + LOCKFILE: ${{ steps.fixture.outputs.lockfile }} |
| 159 | + LOCK_CONTENT: ${{ steps.fixture.outputs.lock-content }} |
| 160 | + with: |
| 161 | + script: | |
| 162 | + const assert = require('node:assert/strict'); |
| 163 | + const fs = require('node:fs'); |
| 164 | + const path = require('node:path'); |
| 165 | + const expected = JSON.parse(process.env.EXPECTED); |
| 166 | + assert.equal(process.env.OUTCOME, expected === 'failure' ? 'failure' : 'success'); |
| 167 | + if (expected !== 'failure') assert.deepEqual(JSON.parse(process.env.DIRECTORIES), expected); |
| 168 | + const installed = await (await glob.create(`${process.env.PROJECT}/**/node_modules`, { followSymbolicLinks: false })).glob(); |
| 169 | + assert.deepEqual(installed, []); |
| 170 | + assert.equal(fs.existsSync(path.join(process.env.PROJECT, 'package.tgz')), false); |
| 171 | + assert.equal(fs.readFileSync(process.env.LOCKFILE, 'utf8'), process.env.LOCK_CONTENT); |
0 commit comments