|
52 | 52 | echo "setup-node outputs run-script-command is not valid" |
53 | 53 | exit 1 |
54 | 54 | fi |
| 55 | +
|
| 56 | + - name: Arrange - Local dependency and project with installation hooks |
| 57 | + id: lifecycle-fixture |
| 58 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 59 | + env: |
| 60 | + SOURCE_DIRECTORY: ${{ matrix.working-directory }} |
| 61 | + RUN_SCRIPT_COMMAND: ${{ steps.act-setup-node.outputs.run-script-command }} |
| 62 | + with: |
| 63 | + script: | |
| 64 | + const fs = require('node:fs'); |
| 65 | + const path = require('node:path'); |
| 66 | + const directory = fs.mkdtempSync(path.join(process.env.GITHUB_WORKSPACE, 'install-hooks-')); |
| 67 | + const dependency = path.join(directory, 'dependency'); |
| 68 | + fs.mkdirSync(dependency); |
| 69 | + const hook = `node -e "require('node:fs').writeFileSync('install-hook-ran', '')"`; |
| 70 | + fs.writeFileSync(path.join(dependency, 'package.json'), JSON.stringify({ |
| 71 | + name: 'lifecycle-dependency', version: '1.0.0', scripts: { postinstall: hook }, |
| 72 | + })); |
| 73 | + await exec.exec('npm', ['pack', '--ignore-scripts', '--pack-destination', directory], { cwd: dependency }); |
| 74 | + const source = JSON.parse(fs.readFileSync(path.join(process.env.SOURCE_DIRECTORY, 'package.json'), 'utf8')); |
| 75 | + fs.writeFileSync(path.join(directory, 'package.json'), JSON.stringify({ |
| 76 | + name: 'lifecycle-project', version: '1.0.0', private: true, |
| 77 | + packageManager: source.packageManager, |
| 78 | + dependencies: { 'lifecycle-dependency': 'file:./lifecycle-dependency-1.0.0.tgz' }, |
| 79 | + scripts: { postinstall: hook, build: `node -e "require('node:fs').writeFileSync('build-ran', '')"` }, |
| 80 | + })); |
| 81 | + fs.copyFileSync('.node-version', path.join(directory, '.node-version')); |
| 82 | + const manager = process.env.RUN_SCRIPT_COMMAND.split(' ')[0]; |
| 83 | + if (manager === 'pnpm') { |
| 84 | + const { stdout } = await exec.getExecOutput('pnpm', ['--version']); |
| 85 | + const approval = parseInt(stdout, 10) < 11 ? 'lifecycle-dependency' : 'lifecycle-dependency@file:lifecycle-dependency-1.0.0.tgz'; |
| 86 | + fs.writeFileSync(path.join(directory, 'pnpm-workspace.yaml'), `allowBuilds:\n ${approval}: true\n`); |
| 87 | + } |
| 88 | + const installArgs = ['install', '--ignore-scripts']; |
| 89 | + if (manager === 'pnpm') installArgs.push('--no-frozen-lockfile'); |
| 90 | + await exec.exec(manager, installArgs, { cwd: directory }); |
| 91 | + fs.rmSync(path.join(directory, 'node_modules'), { recursive: true, force: true }); |
| 92 | + core.setOutput('directory', directory); |
| 93 | +
|
| 94 | + - name: Act - Install with default script policy |
| 95 | + id: default-install |
| 96 | + uses: $/actions/setup-node |
| 97 | + with: |
| 98 | + working-directory: ${{ steps.lifecycle-fixture.outputs.directory }} |
| 99 | + |
| 100 | + - name: Assert - Installation hooks are disabled and explicit builds still run |
| 101 | + working-directory: ${{ steps.lifecycle-fixture.outputs.directory }} |
| 102 | + env: |
| 103 | + RUN_SCRIPT_COMMAND: ${{ steps.default-install.outputs.run-script-command }} |
| 104 | + run: | |
| 105 | + test ! -f install-hook-ran |
| 106 | + test ! -f node_modules/lifecycle-dependency/install-hook-ran |
| 107 | + $RUN_SCRIPT_COMMAND build |
| 108 | + test -f build-ran |
| 109 | + rm -rf node_modules |
| 110 | +
|
| 111 | + - name: Act - Opt in to installation hooks |
| 112 | + uses: $/actions/setup-node |
| 113 | + with: |
| 114 | + working-directory: ${{ steps.lifecycle-fixture.outputs.directory }} |
| 115 | + ignore-scripts: "false" |
| 116 | + |
| 117 | + - name: Assert - Project and approved dependency hooks ran |
| 118 | + working-directory: ${{ steps.lifecycle-fixture.outputs.directory }} |
| 119 | + run: | |
| 120 | + test -f install-hook-ran |
| 121 | + test -f node_modules/lifecycle-dependency/install-hook-ran |
0 commit comments