From cf1b02a2e31362b2c87cc90f0eb9a551498042d9 Mon Sep 17 00:00:00 2001 From: Konrad Pabjan Date: Thu, 17 Aug 2023 09:37:45 -0400 Subject: [PATCH] Add tests for E2E artifact upload --- .github/workflows/artifact-tests.yml | 39 +++---------------- .../__tests__/ci-test-action/action.yml | 5 +++ .../__tests__/ci-test-action/index.js | 14 +++++++ packages/artifact/package.json | 4 +- 4 files changed, 26 insertions(+), 36 deletions(-) create mode 100644 packages/artifact/__tests__/ci-test-action/action.yml create mode 100644 packages/artifact/__tests__/ci-test-action/index.js diff --git a/.github/workflows/artifact-tests.yml b/.github/workflows/artifact-tests.yml index 1ebfa7c5..ad702af5 100644 --- a/.github/workflows/artifact-tests.yml +++ b/.github/workflows/artifact-tests.yml @@ -50,48 +50,19 @@ jobs: - name: Set artifact file contents shell: bash run: | - echo "non-gzip-artifact-content=hello" >> $GITHUB_ENV - echo "gzip-artifact-content=Some large amount of text that has a compression ratio that is greater than 100%. If greater than 100%, gzip is used to upload the file" >> $GITHUB_ENV - echo "empty-artifact-content=_EMPTY_" >> $GITHUB_ENV + echo "file1=hello from file 1" >> $GITHUB_ENV + echo "file2=hello from file 2" >> $GITHUB_ENV - name: Create files that will be uploaded run: | mkdir artifact-path - echo '${{ env.non-gzip-artifact-content }}' > artifact-path/world.txt - echo '${{ env.gzip-artifact-content }}' > artifact-path/gzip.txt - touch artifact-path/empty.txt + echo '${{ env.file1 }}' > artifact-path/first.txt + echo '${{ env.file2 }}' > artifact-path/second.txt # We're using node -e to call the functions directly available in the @actions/artifact package - name: Upload artifacts using uploadArtifact() run: | - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-1',['artifact-path/world.txt'], process.argv[1]))" "${{ github.workspace }}" - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-2',['artifact-path/gzip.txt'], process.argv[1]))" "${{ github.workspace }}" - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-3',['artifact-path/empty.txt'], process.argv[1]))" "${{ github.workspace }}" + node -e "Promise.resolve(require('./packages/artifact/lib/artifact').create().uploadArtifact('my-artifact',['artifact-path/first.txt','artifact-path/second.txt'], process.argv[1]))" "${{ github.workspace }}" - - name: Download artifacts using downloadArtifact() - run: | - mkdir artifact-1-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-1','artifact-1-directory'))" - mkdir artifact-2-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-2','artifact-2-directory'))" - mkdir artifact-3-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-3','artifact-3-directory'))" - - name: Verify downloadArtifact() - shell: bash - run: | - packages/artifact/__tests__/test-artifact-file.sh "artifact-1-directory/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "artifact-2-directory/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "artifact-3-directory/artifact-path/empty.txt" "${{ env.empty-artifact-content }}" - - name: Download artifacts using downloadAllArtifacts() - run: | - mkdir multi-artifact-directory - node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadAllArtifacts('multi-artifact-directory'))" - - - name: Verify downloadAllArtifacts() - shell: bash - run: | - packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-1/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-2/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}" - packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-3/artifact-path/empty.txt" "${{ env.empty-artifact-content }}" diff --git a/packages/artifact/__tests__/ci-test-action/action.yml b/packages/artifact/__tests__/ci-test-action/action.yml new file mode 100644 index 00000000..0a418ae9 --- /dev/null +++ b/packages/artifact/__tests__/ci-test-action/action.yml @@ -0,0 +1,5 @@ +name: 'Set env variables' +description: 'Sets certain env variables so that e2e artifact upload, list, get and download can be tested in a shell' +runs: + using: 'node12' + main: 'index.js' \ No newline at end of file diff --git a/packages/artifact/__tests__/ci-test-action/index.js b/packages/artifact/__tests__/ci-test-action/index.js new file mode 100644 index 00000000..01132ddd --- /dev/null +++ b/packages/artifact/__tests__/ci-test-action/index.js @@ -0,0 +1,14 @@ +// Certain env variables are not set by default in a shell context and are only available in a node context from a running action +// In order to be able to upload and download artifacts e2e in a shell when running CI tests, we need these env variables set +const fs = require('fs'); +const os = require('os'); +const filePath = process.env[`GITHUB_ENV`] +fs.appendFileSync(filePath, `ACTIONS_RESULTS_URL=${process.env.ACTIONS_RESULTS_URL}${os.EOL}`, { + encoding: 'utf8' +}) +fs.appendFileSync(filePath, `ACTIONS_RUNTIME_TOKEN=${process.env.ACTIONS_RUNTIME_TOKEN}${os.EOL}`, { + encoding: 'utf8' +}) +fs.appendFileSync(filePath, `GITHUB_RUN_ID=${process.env.GITHUB_RUN_ID}${os.EOL}`, { + encoding: 'utf8' +}) \ No newline at end of file diff --git a/packages/artifact/package.json b/packages/artifact/package.json index 25fbc984..9845862e 100644 --- a/packages/artifact/package.json +++ b/packages/artifact/package.json @@ -10,8 +10,8 @@ ], "homepage": "https://github.com/actions/toolkit/tree/main/packages/artifact", "license": "MIT", - "main": "lib/artifact-client.js", - "types": "lib/artifact-client.d.ts", + "main": "lib/artifact.js", + "types": "lib/artifact.d.ts", "directories": { "lib": "lib", "test": "__tests__"