mirror of
https://github.com/opentofu/setup-opentofu.git
synced 2026-11-27 10:44:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
847eaa4afe | ||
|
|
34bf4e3161 | ||
|
|
4bcfe8cded | ||
|
|
03a9076efe | ||
|
|
ba1f8b8e04 | ||
|
|
26ccb9be2d | ||
|
|
b572e07b01 | ||
|
|
4959f8f18c | ||
|
|
fc711fa910 | ||
|
|
9d84900f32 | ||
|
|
9e9a1c276b | ||
|
|
274fe8537f | ||
|
|
e95ccdd206 | ||
|
|
04ba7507e2 | ||
|
|
720194ca9d | ||
|
|
4738a2ed5e | ||
|
|
bbaf94fb8f | ||
|
|
dc6f440014 | ||
|
|
8ff58c1228 | ||
|
|
339b7b2a1b | ||
|
|
5e2b22fba4 | ||
|
|
dd259403c5 | ||
|
|
5a7ce099f4 | ||
|
|
58cf7f7458 | ||
|
|
1631e5f6a3 | ||
|
|
82a7ffce68 | ||
|
|
9cc1438d52 | ||
|
|
d23989bcfc | ||
|
|
868f16c8cb | ||
|
|
9fc82761c6 | ||
|
|
b16e63e2c6 | ||
|
|
0f0911fca8 | ||
|
|
000eeb8522 | ||
|
|
4a98c1282d | ||
|
|
592200bd4b | ||
|
|
12f4debbf6 | ||
|
|
ae80d4ecaa | ||
|
|
d00c6817f1 | ||
|
|
da3271e0a2 | ||
|
|
24b9f144af | ||
|
|
05c2a037e9 | ||
|
|
b06654f7ba | ||
|
|
6cdd07fc43 | ||
|
|
5f01fa71eb | ||
|
|
8767493626 | ||
|
|
abd9f2b70f | ||
|
|
595213afdb |
No files matched your search
@@ -1,6 +1,14 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "npm"
|
||||
cooldown:
|
||||
default-days: 7
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
- package-ecosystem: "github-actions"
|
||||
cooldown:
|
||||
default-days: 7
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
@@ -1,19 +1,19 @@
|
||||
name: 'Continuous Integration'
|
||||
name: "Continuous Integration"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- main
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
check-dist:
|
||||
name: Check dist/ directory
|
||||
uses: actions/reusable-workflows/.github/workflows/check-dist.yml@967035ce963867fb956a309c9b67512314bc7c1f
|
||||
uses: actions/reusable-workflows/.github/workflows/check-dist.yml@2826fb8353263a138210fc017301ce5767a9c0d4
|
||||
with:
|
||||
node-version: "20.x"
|
||||
node-version: "24.14.0"
|
||||
test:
|
||||
name: Test
|
||||
uses: actions/reusable-workflows/.github/workflows/basic-validation.yml@967035ce963867fb956a309c9b67512314bc7c1f
|
||||
uses: actions/reusable-workflows/.github/workflows/basic-validation.yml@2826fb8353263a138210fc017301ce5767a9c0d4
|
||||
with:
|
||||
node-version: "20.x"
|
||||
node-version: "24.14.0"
|
||||
@@ -0,0 +1 @@
|
||||
latest
|
||||
@@ -0,0 +1,21 @@
|
||||
name: Retag
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v[1-2].[0-9]+.[0-9]+*'
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
|
||||
jobs:
|
||||
tag:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v6
|
||||
- run: |
|
||||
set -o pipefail
|
||||
git tag --force "$(grep '^v[1-2]\.[0-9]\+\.[0-9]\+\(.*\)\?$' <<< ${GITHUB_REF#refs/*/} | sed 's/^v\([1-2]\)\..*/v\1/')"
|
||||
git push --tag --force
|
||||
|
||||
@@ -10,18 +10,40 @@ defaults:
|
||||
run:
|
||||
shell: bash
|
||||
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
jobs:
|
||||
tofu-version-files:
|
||||
name: 'OpenTofu Version Files'
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
tofu-version-files: ['./.github/workflows/data/local/.opentofu-version']
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Setup OpenTofu - ${{ matrix['tofu-version-files'] }}
|
||||
uses: ./
|
||||
with:
|
||||
tofu_version_file: ${{ matrix['tofu-version-files'] }}
|
||||
tofu_wrapper: false
|
||||
|
||||
- name: Validate that OpenTofu was installed
|
||||
run: tofu version | grep 'OpenTofu v'
|
||||
tofu-versions:
|
||||
name: 'OpenTofu Versions'
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
tofu-versions: [1.6.0-alpha1, latest]
|
||||
tofu-versions: [1.6.0, latest]
|
||||
tofu-wrapper: [true, false]
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Setup OpenTofu - ${{ matrix['tofu-versions'] }}
|
||||
uses: ./
|
||||
@@ -44,7 +66,7 @@ jobs:
|
||||
tofu-wrapper: [true, false]
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Setup OpenTofu
|
||||
uses: ./
|
||||
@@ -77,10 +99,11 @@ jobs:
|
||||
tofu-wrapper: [true, false]
|
||||
defaults:
|
||||
run:
|
||||
shell: bash
|
||||
working-directory: ./.github/workflows/data/local
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Setup OpenTofu
|
||||
uses: ./
|
||||
@@ -110,7 +133,7 @@ jobs:
|
||||
TF_CLOUD_API_TOKEN: 'XXXXXXXXXXXXXX.atlasv1.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Setup OpenTofu
|
||||
uses: ./
|
||||
@@ -139,7 +162,7 @@ jobs:
|
||||
TF_CLOUD_API_TOKEN: 'XXXXXXXXXXXXXX.atlasv1.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Setup OpenTofu
|
||||
uses: ./
|
||||
@@ -167,7 +190,7 @@ jobs:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v6
|
||||
|
||||
- name: Setup OpenTofu
|
||||
uses: ./
|
||||
@@ -181,4 +204,3 @@ jobs:
|
||||
if: runner.os != 'Windows'
|
||||
run: |
|
||||
[[ -f ${HOME}/.tofurc ]] || exit 0
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
#!/bin/sh
|
||||
. "$(dirname "$0")/_/husky.sh"
|
||||
|
||||
npm run lint || (npm install && npm run lint)
|
||||
npm run test
|
||||
npm audit --audit-level=high
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
## Unreleased
|
||||
|
||||
Initial release. It provides feature parity with the [setup-terraform](https://github.com/hashicorp/setup-terraform) action.
|
||||
+6
-1
@@ -4,4 +4,9 @@
|
||||
# We currently do not have any specific code owners
|
||||
# In the future, we will have a Github team of global code owners of the entire package
|
||||
# Later on, we will start splitting up the responsibilities, and packages will be assigned more specific code owners
|
||||
# * @opentofu-code-owners
|
||||
* @opentofu/maintainers
|
||||
|
||||
# The last matching pattern takes the most precedence for CODEOWNERS. CODEOWNERS does not have fine-grained control so we will
|
||||
# just match whole changes for these specific files, but @diofeher is responsible for taking care of the Dependabot updates
|
||||
# in this repository.
|
||||
package*.json @diofeher
|
||||
@@ -1,10 +0,0 @@
|
||||
# Code of Conduct
|
||||
|
||||
We follow the [CNCF Code of Conduct](https://github.com/cncf/foundation/blob/main/code-of-conduct.md).
|
||||
|
||||
<!-- TODO: Decide who will handle Code of Conduct reports and replace [INSERT EMAIL ADDRESS]
|
||||
with an email address in the paragraph below. We recommend using a mailing list to handle reports.
|
||||
If your project isn't prepared to handle reports, remove the project email address and just have
|
||||
reporters send to conduct@cncf.io.
|
||||
-->
|
||||
Please contact contact@opentf.org in order to report violations of the Code of Conduct.
|
||||
@@ -1,18 +1,12 @@
|
||||
# setup-opentofu
|
||||
# GitHub Action for setting up OpenTofu
|
||||
|
||||
The `opentofu/setup-opentofu` action sets up OpenTofu CLI in your GitHub Actions workflow by:
|
||||
> [!NOTE]
|
||||
> This is a community-maintained repository. The OpenTofu team does not fix non-critical bugs or add features, but is happy to review community pull requests.
|
||||
|
||||
- Downloading the latest version of OpenTofu CLI and adding it to the `PATH`.
|
||||
- Configuring the [CLI configuration file](https://opentofu.org/docs/cli/config/config-file/) with a Terraform Cloud/Enterprise hostname and API token.
|
||||
- Installing a wrapper script to wrap subsequent calls of the `tofu` binary and expose its STDOUT, STDERR, and exit code as outputs named `stdout`, `stderr`, and `exitcode` respectively. (This can be optionally skipped if subsequent steps in the same job do not need to access the results of
|
||||
OpenTofu commands.)
|
||||
> [!TIP]
|
||||
> Having trouble with exit codes or the output format? Try setting the `tofu_wrapper` setting to `false`.
|
||||
|
||||
After you've used the action, subsequent steps in the same job can run arbitrary OpenTofu commands using [the GitHub Actions `run` syntax](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun). This allows most OpenTofu commands to work exactly
|
||||
like they do on your local command line.
|
||||
|
||||
## Experimental Status
|
||||
|
||||
By using the software in this repository (the "Software"), you acknowledge that: (1) the Software is still in development, may change, and has not been released as a commercial product by OpenTofu; (2) the Software is provided on an "as-is" basis, and may include bugs, errors, or other issues; (3) the Software is NOT INTENDED FOR PRODUCTION USE, use of the Software may result in unexpected results, loss of data, or other unexpected results, and OpenTofu disclaims any and all liability resulting from use of the Software.
|
||||
The `opentofu/setup-opentofu` action sets up OpenTofu CLI in GitHub Actions.
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -22,23 +16,34 @@ The default configuration installs the latest version of OpenTofu CLI and instal
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
```
|
||||
|
||||
A specific version of OpenTofu CLI can be installed:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
tofu_version: 1.6.0-alpha1
|
||||
tofu_version: 1.6.0
|
||||
```
|
||||
|
||||
You can also specify the version in a file (e.g., `.opentofu-version`):
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
tofu_version_file: .opentofu-version
|
||||
```
|
||||
|
||||
Supported version syntax is the same as for the `tofu_version` input. If both `tofu_version` and `tofu_version_file` are provided, the version number in the file takes precedence.
|
||||
|
||||
Credentials for Terraform Cloud ([app.terraform.io](https://app.terraform.io/)) can be configured:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
|
||||
```
|
||||
@@ -47,7 +52,7 @@ Credentials for Terraform Enterprise (TFE) can be configured:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
cli_config_credentials_hostname: 'tofu.example.com'
|
||||
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
|
||||
@@ -57,16 +62,49 @@ The wrapper script installation can be skipped by setting the `tofu_wrapper` var
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
tofu_wrapper: false
|
||||
```
|
||||
|
||||
Caching can be enabled to reduce download time on subsequent workflow runs by storing the OpenTofu binary using the GitHub Actions [tool-cache](https://github.com/actions/toolkit/tree/main/packages/tool-cache). Note that this feature is primarily useful for self-hosted runners that support toolchain caching, since GitHub-hosted runners are ephemeral and do not persist the tool cache between runs:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
cache: true
|
||||
```
|
||||
|
||||
Provider acceptance test environment variables can be configured automatically by setting `provider_acceptance_tests` to `true`. This exports `TF_ACC`, `TF_ACC_PROVIDER_NAMESPACE`, `TF_ACC_PROVIDER_HOST`, and `TF_ACC_TERRAFORM_PATH` so you don't need to set them manually:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
tofu_wrapper: false
|
||||
provider_acceptance_tests: true
|
||||
- run: go mod download
|
||||
- run: go test -v -cover ./...
|
||||
timeout-minutes: 10
|
||||
```
|
||||
|
||||
Validation can be enabled to check the downloaded OpenTofu CLI SHA256 hash against a newline-delimited list of checksums:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
with:
|
||||
checksums: |
|
||||
933b060ab1cf05b106e94af1d370fd14b3006a6845495a67c68734269cc705ad
|
||||
d3d29f51e75a701fc7cf67c0644a8c883a85f36cf1621461988baffd88e7f361
|
||||
```
|
||||
|
||||
Subsequent steps can access outputs when the wrapper script is installed:
|
||||
|
||||
```yaml
|
||||
steps:
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
|
||||
- run: tofu init
|
||||
|
||||
@@ -93,8 +131,8 @@ defaults:
|
||||
permissions:
|
||||
pull-requests: write
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: actions/checkout@v6
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
|
||||
- name: OpenTofu fmt
|
||||
id: fmt
|
||||
@@ -161,8 +199,8 @@ defaults:
|
||||
permissions:
|
||||
pull-requests: write
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: opentofu/setup-opentofu
|
||||
- uses: actions/checkout@v6
|
||||
- uses: opentofu/setup-opentofu@v2
|
||||
|
||||
- name: OpenTofu fmt
|
||||
id: fmt
|
||||
@@ -254,9 +292,18 @@ The action supports the following inputs:
|
||||
for available range specifications). Examples are: `<1.6.0-beta`, `~1.6.0-alpha`, `1.6.0-alpha2` (all three installing
|
||||
the latest available `1.6.0-alpha2` version). Prerelease versions can be specified and a range will stay within the
|
||||
given tag such as `beta` or `rc`. If no version is given, it will default to `latest`.
|
||||
- `tofu_version_file` - (optional) Path to a file containing the OpenTofu version to install. Supported version syntax
|
||||
is the same as for the `tofu_version` input. Takes precedence over `tofu_version` if both are provided.
|
||||
- `tofu_wrapper` - (optional) Whether to install a wrapper to wrap subsequent calls of
|
||||
the `tofu` binary and expose its STDOUT, STDERR, and exit code as outputs
|
||||
named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.
|
||||
- `cache` - (optional) Whether to use GitHub Actions tool-cache to store and reuse downloaded OpenTofu binaries. Defaults to `false`.
|
||||
- `provider_acceptance_tests` - (optional) Whether to automatically set environment variables for running
|
||||
OpenTofu provider acceptance tests. When set to `true`, the following environment variables are exported:
|
||||
`TF_ACC=1`, `TF_ACC_PROVIDER_NAMESPACE=hashicorp`, `TF_ACC_PROVIDER_HOST=registry.opentofu.org`, and
|
||||
`TF_ACC_TERRAFORM_PATH=<path to tofu binary>`. Defaults to `false`.
|
||||
- `github_token` - (optional) Override the GitHub token read from the environment variable. Defaults to the value of the `GITHUB_TOKEN` environment variable unless running on Forgejo or Gitea.
|
||||
- `checksums` - (optional) A newline-delimited list of valid checksums (SHA256) for the downloaded OpenTofu CLI ZIP. When set, the action will verify the ZIP matches one of the checksums before proceeding. Defaults to `[]`
|
||||
|
||||
## Outputs
|
||||
|
||||
@@ -271,6 +318,3 @@ to `true`, the following outputs are available for subsequent steps that call th
|
||||
|
||||
[Mozilla Public License v2.0](LICENSE)
|
||||
|
||||
## Code of Conduct
|
||||
|
||||
[Code of Conduct](CODE_OF_CONDUCT.md)
|
||||
+21
-3
@@ -13,10 +13,28 @@ inputs:
|
||||
description: 'The version of OpenTofu CLI to install. If no version is given, it will default to `latest`.'
|
||||
default: 'latest'
|
||||
required: false
|
||||
tofu_version_file:
|
||||
description: 'Path to a file containing the OpenTofu version to install. Takes precedence over `tofu_version` if both are provided.'
|
||||
required: false
|
||||
tofu_wrapper:
|
||||
description: 'Whether or not to install a wrapper to wrap subsequent calls of the `tofu` binary and expose its STDOUT, STDERR, and exit code as outputs named `stdout`, `stderr`, and `exitcode` respectively. Defaults to `true`.'
|
||||
default: 'true'
|
||||
required: false
|
||||
cache:
|
||||
description: 'Whether to use GitHub Actions tool-cache to store and reuse downloaded OpenTofu binaries. Defaults to `false`.'
|
||||
default: 'false'
|
||||
required: false
|
||||
github_token:
|
||||
description: 'API token for GitHub to increase the rate limit. Defaults to the GITHUB_TOKEN environment variable unless running on Forgejo/Gitea.'
|
||||
default: ''
|
||||
required: false
|
||||
provider_acceptance_tests:
|
||||
description: 'Whether to automatically set environment variables for running OpenTofu provider acceptance tests (TF_ACC, TF_ACC_PROVIDER_NAMESPACE, TF_ACC_PROVIDER_HOST, TF_ACC_TERRAFORM_PATH). Defaults to `false`.'
|
||||
default: 'false'
|
||||
required: false
|
||||
checksums:
|
||||
description: Newline-delimited list of valid checksums (SHA256 hashes) for the downloaded OpenTofu binary. When set, the action will verify that the binary matches one of these checksums before proceeding.
|
||||
required: false
|
||||
outputs:
|
||||
stdout:
|
||||
description: 'The STDOUT stream of the call to the `tofu` binary.'
|
||||
@@ -29,8 +47,8 @@ outputs:
|
||||
value: '126'
|
||||
|
||||
branding:
|
||||
icon: 'terminal'
|
||||
color: 'purple'
|
||||
icon: 'box'
|
||||
color: 'yellow'
|
||||
runs:
|
||||
using: 'node20'
|
||||
using: 'node24'
|
||||
main: 'dist/index.js'
|
||||
Vendored
+32261
-7649
File diff suppressed because it is too large.
Load diff
Vendored
+30537
-3794
File diff suppressed because it is too large.
Load diff
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
@@ -1,16 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
const core = require('@actions/core');
|
||||
import { setFailed, debug } from '@actions/core';
|
||||
|
||||
const setup = require('./lib/setup-tofu');
|
||||
import setup from './lib/setup-tofu.js';
|
||||
import { getErrorMessage, getErrorDetail } from './lib/error-utils.js';
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await setup();
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
const message = getErrorMessage(error);
|
||||
debug(getErrorDetail(error));
|
||||
setFailed(message);
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* Normalizes errors (including AggregateError from fetch/undici) into
|
||||
* a single, actionable message for GitHub Actions logs.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get a user-friendly message from any error.
|
||||
* - AggregateError: flattens error.errors so network/download causes are visible.
|
||||
* - Standard Error: returns message; includes cause if present (Node 16+).
|
||||
*
|
||||
* @param {unknown} error - Caught value (Error, AggregateError, or other).
|
||||
* @returns {string} Single-line message suitable for core.setFailed().
|
||||
*/
|
||||
function getErrorMessage (error) {
|
||||
if (error instanceof AggregateError && Array.isArray(error.errors)) {
|
||||
if (error.errors.length === 0) {
|
||||
return 'AggregateError (one or more operations failed)';
|
||||
}
|
||||
const parts = error.errors.map((e) => (e && typeof e.message === 'string' ? e.message : String(e)));
|
||||
const combined = parts.join('; ');
|
||||
return combined || 'AggregateError (one or more operations failed)';
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
if (error.cause instanceof Error) {
|
||||
return `${error.message}: ${error.cause.message}`;
|
||||
}
|
||||
return error.message;
|
||||
}
|
||||
|
||||
return String(error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a detailed string for logging (e.g. core.debug or core.error).
|
||||
* Includes stack and, for AggregateError, each nested error.
|
||||
*
|
||||
* @param {unknown} error - Caught value.
|
||||
* @returns {string} Multi-line detail string.
|
||||
*/
|
||||
function getErrorDetail (error) {
|
||||
const lines = [];
|
||||
|
||||
if (error instanceof AggregateError && Array.isArray(error.errors)) {
|
||||
lines.push(`AggregateError (${error.errors.length} error(s)):`);
|
||||
error.errors.forEach((e, i) => {
|
||||
lines.push(` [${i + 1}] ${e instanceof Error ? e.message : String(e)}`);
|
||||
if (e instanceof Error && e.stack) {
|
||||
lines.push(e.stack.split('\n').map((l) => ' ' + l).join('\n'));
|
||||
}
|
||||
});
|
||||
if (error.stack) {
|
||||
lines.push('Outer stack:');
|
||||
lines.push(error.stack);
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
lines.push(error.message);
|
||||
if (error.stack) lines.push(error.stack);
|
||||
if (error.cause) {
|
||||
lines.push('Caused by:');
|
||||
lines.push(getErrorDetail(error.cause));
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
return String(error);
|
||||
}
|
||||
|
||||
export { getErrorMessage, getErrorDetail };
|
||||
+82
-34
@@ -1,24 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
import { HttpClient, HttpCodes } from '@actions/http-client';
|
||||
import semver from 'semver';
|
||||
import { getErrorMessage } from './error-utils.js';
|
||||
|
||||
class Build {
|
||||
constructor (name, url) {
|
||||
constructor (version, name) {
|
||||
this.name = name;
|
||||
this.url = url;
|
||||
this.url = `https://github.com/opentofu/opentofu/releases/download/v${version}/${name}`;
|
||||
}
|
||||
}
|
||||
|
||||
class Release {
|
||||
constructor (releaseMeta) {
|
||||
this.version = releaseMeta.tag_name.replace('v', '');
|
||||
this.builds = releaseMeta.assets.map(asset => new Build(asset.name, asset.browser_download_url));
|
||||
this.version = releaseMeta.id.replace('v', '');
|
||||
this.builds = releaseMeta.files.map(
|
||||
(asset) => new Build(this.version, asset)
|
||||
);
|
||||
}
|
||||
|
||||
getBuild (platform, arch) {
|
||||
const requiredName = `tofu_${this.version}_${platform}_${arch}.zip`;
|
||||
return this.builds.find(build => build.name === requiredName);
|
||||
return this.builds.find((build) => build.name === requiredName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,74 +34,115 @@ class Release {
|
||||
*
|
||||
* @return {Array<Release>} Releases.
|
||||
*/
|
||||
async function fetchReleases () {
|
||||
const url = 'https://api.github.com/repos/opentofu/opentofu/releases';
|
||||
async function fetchReleases (githubToken) {
|
||||
const userAgent = 'opentofu/setup-opentofu';
|
||||
|
||||
const resp = await fetch(url, {
|
||||
headers: {
|
||||
Accept: 'application/vnd.github+json',
|
||||
'X-GitHub-Api-Version': '2022-11-28'
|
||||
}
|
||||
});
|
||||
const http = new HttpClient(userAgent);
|
||||
|
||||
if (!resp.ok) {
|
||||
throw new Error('failed fetching releases');
|
||||
const url = 'https://get.opentofu.org/tofu/api.json';
|
||||
|
||||
const headers = {
|
||||
Accept: 'application/json'
|
||||
};
|
||||
|
||||
let resp;
|
||||
try {
|
||||
resp = await http.get(url, headers);
|
||||
} catch (error) {
|
||||
const cause = getErrorMessage(error);
|
||||
throw new Error(`Failed to fetch OpenTofu releases from ${url}: ${cause}`);
|
||||
}
|
||||
|
||||
const releasesMeta = await resp.json();
|
||||
if (resp.message.statusCode !== HttpCodes.OK) {
|
||||
throw new Error(
|
||||
'failed fetching releases (' + resp.message.statusCode + ')'
|
||||
);
|
||||
}
|
||||
|
||||
return releasesMeta.map(releaseMeta => new Release(releaseMeta));
|
||||
let body;
|
||||
try {
|
||||
body = await resp.readBody();
|
||||
} catch (error) {
|
||||
const cause = getErrorMessage(error);
|
||||
throw new Error(`Failed to read releases response: ${cause}`);
|
||||
}
|
||||
|
||||
let releasesMeta;
|
||||
try {
|
||||
releasesMeta = JSON.parse(body);
|
||||
} catch (error) {
|
||||
const cause = getErrorMessage(error);
|
||||
throw new Error(`Invalid releases JSON from ${url}: ${cause}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Array}
|
||||
*/
|
||||
const versions = releasesMeta.versions;
|
||||
|
||||
return versions.map((releaseMeta) => new Release(releaseMeta));
|
||||
}
|
||||
|
||||
const semver = require('semver');
|
||||
|
||||
async function findLatestVersion (versions) {
|
||||
return versions.sort((a, b) => semver.rcompare(a, b))[0];
|
||||
return versions
|
||||
.filter((v) => semver.prerelease(v) === null)
|
||||
.sort((a, b) => semver.rcompare(a, b))[0];
|
||||
}
|
||||
|
||||
async function findLatestVersionInRange (versions, range) {
|
||||
return semver.maxSatisfying(versions, range, { prerelease: true, loose: true });
|
||||
return semver.maxSatisfying(versions, range, {
|
||||
prerelease: true,
|
||||
loose: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the release given the version.
|
||||
*
|
||||
* @param {string} version: Release version.
|
||||
* @param {string} githubToken: GitHub token to use for working around rate limits.
|
||||
* @param {function} fetchReleasesFn: Optional function to fetch releases.
|
||||
* @return {Release} Release.
|
||||
*/
|
||||
async function getRelease (version, fetchReleasesFn = fetchReleases) {
|
||||
async function getRelease (
|
||||
version,
|
||||
githubToken,
|
||||
fetchReleasesFn = fetchReleases
|
||||
) {
|
||||
const latestVersionLabel = 'latest';
|
||||
|
||||
const versionsRange = semver.validRange(version, { prerelease: true, loose: true });
|
||||
const versionsRange = semver.validRange(version, {
|
||||
prerelease: true,
|
||||
loose: true
|
||||
});
|
||||
if (versionsRange === null && version !== latestVersionLabel) {
|
||||
throw new Error('Input version cannot be used, see semver: https://semver.org/spec/v2.0.0.html');
|
||||
throw new Error(
|
||||
'Input version cannot be used, see semver: https://semver.org/spec/v2.0.0.html'
|
||||
);
|
||||
}
|
||||
|
||||
const releases = await fetchReleasesFn();
|
||||
const releases = await fetchReleasesFn(githubToken);
|
||||
|
||||
if (releases === null || releases.length === 0) {
|
||||
throw new Error('No tofu releases found, please contact OpenTofu');
|
||||
}
|
||||
|
||||
const versionsFound = releases.map(release => release.version);
|
||||
const versionsFound = releases.map((release) => release.version);
|
||||
let versionSelected;
|
||||
if (version === latestVersionLabel) {
|
||||
versionSelected = await findLatestVersion(versionsFound);
|
||||
} else {
|
||||
versionSelected = await findLatestVersionInRange(versionsFound, versionsRange);
|
||||
versionSelected = await findLatestVersionInRange(
|
||||
versionsFound,
|
||||
versionsRange
|
||||
);
|
||||
}
|
||||
|
||||
if (versionSelected === null) {
|
||||
throw new Error('No matching version found');
|
||||
}
|
||||
|
||||
return releases.find(release => release.version === versionSelected);
|
||||
return releases.find((release) => release.version === versionSelected);
|
||||
}
|
||||
|
||||
// Note that the export is defined as adaptor to replace hashicorp/js-releases
|
||||
// See: https://github.com/hashicorp/setup-terraform/blob/e192cfcbae6c6ed207c277ed7624131996c9bf13/lib/setup-terraform.js#L15
|
||||
module.exports = {
|
||||
getRelease,
|
||||
Release
|
||||
};
|
||||
export { getRelease, Release };
|
||||
+165
-61
@@ -5,15 +5,29 @@
|
||||
*/
|
||||
|
||||
// Node.js core
|
||||
const fs = require('fs').promises;
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
import { promises as fs } from 'fs';
|
||||
import { platform, arch } from 'os';
|
||||
import { sep, join, resolve, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
// External
|
||||
const core = require('@actions/core');
|
||||
const tc = require('@actions/tool-cache');
|
||||
const io = require('@actions/io');
|
||||
const releases = require('./releases');
|
||||
import {
|
||||
debug,
|
||||
error as logError,
|
||||
exportVariable,
|
||||
getInput,
|
||||
getMultilineInput,
|
||||
warning,
|
||||
addPath
|
||||
} from '@actions/core';
|
||||
import { downloadTool, extractZip, find, cacheDir } from '@actions/tool-cache';
|
||||
import { mv, cp, mkdirP } from '@actions/io';
|
||||
import { getRelease } from './releases.js';
|
||||
import { getErrorMessage } from './error-utils.js';
|
||||
import { fileSHA256 } from './util.js';
|
||||
|
||||
// __dirname is not available in ES modules, so we need to construct it ourselves
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
|
||||
// return value in [amd64, 386, arm]
|
||||
@@ -34,9 +48,29 @@ function mapOS (os) {
|
||||
return os;
|
||||
}
|
||||
|
||||
async function downloadAndExtractCLI (url) {
|
||||
core.debug(`Downloading OpenTofu CLI from ${url}`);
|
||||
const pathToCLIZip = await tc.downloadTool(url);
|
||||
async function downloadAndExtractCLI (url, checksums) {
|
||||
debug(`Downloading OpenTofu CLI from ${url}`);
|
||||
let pathToCLIZip;
|
||||
try {
|
||||
pathToCLIZip = await downloadTool(url);
|
||||
} catch (error) {
|
||||
const cause = getErrorMessage(error);
|
||||
throw new Error(`Failed to download OpenTofu from ${url}: ${cause}`);
|
||||
}
|
||||
|
||||
if (checksums.length > 0) {
|
||||
let checksum;
|
||||
try {
|
||||
checksum = await fileSHA256(pathToCLIZip);
|
||||
} catch (error) {
|
||||
const cause = getErrorMessage(error);
|
||||
throw new Error(`Failed to calculate the checksum for the OpenTofu CLI zip: ${cause}`);
|
||||
}
|
||||
|
||||
if (!checksums.includes(checksum)) {
|
||||
throw new Error(`Failed to validate OpenTofu CLI zip checksum. Received: ${checksum}. Valid checksums: ${checksums.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!pathToCLIZip) {
|
||||
throw new Error(`Unable to download OpenTofu from ${url}`);
|
||||
@@ -44,18 +78,23 @@ async function downloadAndExtractCLI (url) {
|
||||
|
||||
let pathToCLI;
|
||||
|
||||
core.debug('Extracting OpenTofu CLI zip file');
|
||||
if (os.platform().startsWith('win')) {
|
||||
core.debug(`OpenTofu CLI Download Path is ${pathToCLIZip}`);
|
||||
const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
|
||||
io.mv(pathToCLIZip, fixedPathToCLIZip);
|
||||
core.debug(`Moved download to ${fixedPathToCLIZip}`);
|
||||
pathToCLI = await tc.extractZip(fixedPathToCLIZip);
|
||||
} else {
|
||||
pathToCLI = await tc.extractZip(pathToCLIZip);
|
||||
debug('Extracting OpenTofu CLI zip file');
|
||||
try {
|
||||
if (platform().startsWith('win')) {
|
||||
debug(`OpenTofu CLI Download Path is ${pathToCLIZip}`);
|
||||
const fixedPathToCLIZip = `${pathToCLIZip}.zip`;
|
||||
await mv(pathToCLIZip, fixedPathToCLIZip);
|
||||
debug(`Moved download to ${fixedPathToCLIZip}`);
|
||||
pathToCLI = await extractZip(fixedPathToCLIZip);
|
||||
} else {
|
||||
pathToCLI = await extractZip(pathToCLIZip);
|
||||
}
|
||||
} catch (error) {
|
||||
const cause = getErrorMessage(error);
|
||||
throw new Error(`Failed to extract OpenTofu archive: ${cause}`);
|
||||
}
|
||||
|
||||
core.debug(`OpenTofu CLI path is ${pathToCLI}.`);
|
||||
debug(`OpenTofu CLI path is ${pathToCLI}.`);
|
||||
|
||||
if (!pathToCLI) {
|
||||
throw new Error('Unable to unzip OpenTofu');
|
||||
@@ -68,32 +107,34 @@ async function installWrapper (pathToCLI) {
|
||||
let source, target;
|
||||
|
||||
// If we're on Windows, then the executable ends with .exe
|
||||
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
|
||||
const exeSuffix = platform().startsWith('win') ? '.exe' : '';
|
||||
|
||||
// Rename tofu(.exe) to tofu-bin(.exe)
|
||||
try {
|
||||
source = [pathToCLI, `tofu${exeSuffix}`].join(path.sep);
|
||||
target = [pathToCLI, `tofu-bin${exeSuffix}`].join(path.sep);
|
||||
core.debug(`Moving ${source} to ${target}.`);
|
||||
await io.mv(source, target);
|
||||
source = [pathToCLI, `tofu${exeSuffix}`].join(sep);
|
||||
target = [pathToCLI, `tofu-bin${exeSuffix}`].join(sep);
|
||||
debug(`Moving ${source} to ${target}.`);
|
||||
await mv(source, target);
|
||||
} catch (e) {
|
||||
core.error(`Unable to move ${source} to ${target}.`);
|
||||
logError(`Unable to move ${source} to ${target}.`);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Install our wrapper as tofu
|
||||
try {
|
||||
source = path.resolve([__dirname, '..', 'wrapper', 'dist', 'index.js'].join(path.sep));
|
||||
target = [pathToCLI, 'tofu'].join(path.sep);
|
||||
core.debug(`Copying ${source} to ${target}.`);
|
||||
await io.cp(source, target);
|
||||
source = resolve(
|
||||
[__dirname, '..', 'wrapper', 'dist', 'index.js'].join(sep)
|
||||
);
|
||||
target = [pathToCLI, 'tofu'].join(sep);
|
||||
debug(`Copying ${source} to ${target}.`);
|
||||
await cp(source, target);
|
||||
} catch (e) {
|
||||
core.error(`Unable to copy ${source} to ${target}.`);
|
||||
logError(`Unable to copy ${source} to ${target}.`);
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Export a new environment variable, so our wrapper can locate the binary
|
||||
core.exportVariable('TOFU_CLI_PATH', pathToCLI);
|
||||
exportVariable('TOFU_CLI_PATH', pathToCLI);
|
||||
}
|
||||
|
||||
// Add credentials to CLI Configuration File
|
||||
@@ -108,46 +149,99 @@ credentials "${credentialsHostname}" {
|
||||
// eslint-enable
|
||||
|
||||
// default to OS-specific path
|
||||
let credsFile = osPlat === 'win32'
|
||||
? `${process.env.APPDATA}/tofu.rc`
|
||||
: `${process.env.HOME}/.tofurc`;
|
||||
let credsFile =
|
||||
osPlat === 'win32'
|
||||
? `${process.env.APPDATA}/tofu.rc`
|
||||
: `${process.env.HOME}/.tofurc`;
|
||||
|
||||
// override with TF_CLI_CONFIG_FILE environment variable
|
||||
credsFile = process.env.TF_CLI_CONFIG_FILE ? process.env.TF_CLI_CONFIG_FILE : credsFile;
|
||||
credsFile = process.env.TF_CLI_CONFIG_FILE
|
||||
? process.env.TF_CLI_CONFIG_FILE
|
||||
: credsFile;
|
||||
|
||||
// get containing folder
|
||||
const credsFolder = path.dirname(credsFile);
|
||||
const credsFolder = dirname(credsFile);
|
||||
|
||||
core.debug(`Creating ${credsFolder}`);
|
||||
await io.mkdirP(credsFolder);
|
||||
debug(`Creating ${credsFolder}`);
|
||||
await mkdirP(credsFolder);
|
||||
|
||||
core.debug(`Adding credentials to ${credsFile}`);
|
||||
debug(`Adding credentials to ${credsFile}`);
|
||||
await fs.writeFile(credsFile, creds);
|
||||
}
|
||||
|
||||
async function run () {
|
||||
try {
|
||||
// Gather GitHub Actions inputs
|
||||
const version = core.getInput('tofu_version');
|
||||
const credentialsHostname = core.getInput('cli_config_credentials_hostname');
|
||||
const credentialsToken = core.getInput('cli_config_credentials_token');
|
||||
const wrapper = core.getInput('tofu_wrapper') === 'true';
|
||||
|
||||
// Gather OS details
|
||||
const osPlatform = os.platform();
|
||||
const osArch = os.arch();
|
||||
|
||||
core.debug(`Finding releases for OpenTofu version ${version}`);
|
||||
const release = await releases.getRelease(version);
|
||||
const platform = mapOS(osPlatform);
|
||||
const arch = mapArch(osArch);
|
||||
const build = release.getBuild(platform, arch);
|
||||
if (!build) {
|
||||
throw new Error(`OpenTofu version ${version} not available for ${platform} and ${arch}`);
|
||||
let version = getInput('tofu_version');
|
||||
const versionFile = getInput('tofu_version_file');
|
||||
const credentialsHostname = getInput('cli_config_credentials_hostname');
|
||||
const credentialsToken = getInput('cli_config_credentials_token');
|
||||
const wrapper = getInput('tofu_wrapper') === 'true';
|
||||
const providerAcceptanceTest = getInput('provider_acceptance_tests') === 'true';
|
||||
const useCache = getInput('cache') === 'true';
|
||||
const checksums = getMultilineInput('checksums');
|
||||
let githubToken = getInput('github_token');
|
||||
if (
|
||||
githubToken === '' &&
|
||||
!(process.env.FORGEJO_ACTIONS || process.env.GITEA_ACTIONS)
|
||||
) {
|
||||
// Only default to the environment variable when running in GitHub Actions. Don't do this for other CI systems
|
||||
// that may set the GITHUB_TOKEN environment variable.
|
||||
githubToken = process.env.GITHUB_TOKEN;
|
||||
}
|
||||
|
||||
// Download requested version
|
||||
const pathToCLI = await downloadAndExtractCLI(build.url);
|
||||
// If tofu_version_file is provided, read the version from the file
|
||||
if (versionFile) {
|
||||
try {
|
||||
debug(`Reading OpenTofu version from file: ${versionFile}`);
|
||||
const fileVersion = await fs.readFile(versionFile, 'utf8');
|
||||
const trimmedVersion = fileVersion.trim();
|
||||
if (trimmedVersion) {
|
||||
version = trimmedVersion;
|
||||
debug(`Using version from file: ${version}`);
|
||||
} else {
|
||||
warning(
|
||||
`Version file ${versionFile} is empty, using tofu_version input: ${version}`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
warning(
|
||||
`Failed to read version from file ${versionFile}: ${error.message}. Using tofu_version input: ${version}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Gather OS details
|
||||
const osPlatform = platform();
|
||||
const osArch = arch();
|
||||
|
||||
debug(`Finding releases for OpenTofu version ${version}`);
|
||||
const release = await getRelease(version, githubToken);
|
||||
const buildPlatform = mapOS(osPlatform);
|
||||
const buildArch = mapArch(osArch);
|
||||
const build = release.getBuild(buildPlatform, buildArch);
|
||||
if (!build) {
|
||||
throw new Error(
|
||||
`OpenTofu version ${version} not available for ${buildPlatform} and ${buildArch}`
|
||||
);
|
||||
}
|
||||
|
||||
// Download requested version if not cached
|
||||
let pathToCLI;
|
||||
if (useCache) {
|
||||
const cachedPath = find('tofu', release.version, buildArch);
|
||||
if (cachedPath) {
|
||||
debug(`Using cached OpenTofu version ${release.version} from ${cachedPath}`);
|
||||
pathToCLI = cachedPath;
|
||||
} else {
|
||||
debug(`OpenTofu version ${release.version} not found in cache, downloading...`);
|
||||
const extractedPath = await downloadAndExtractCLI(build.url, checksums);
|
||||
debug(`Caching OpenTofu version ${release.version} to tool cache`);
|
||||
pathToCLI = await cacheDir(extractedPath, 'tofu', release.version, buildArch);
|
||||
}
|
||||
} else {
|
||||
pathToCLI = await downloadAndExtractCLI(build.url, checksums);
|
||||
}
|
||||
|
||||
// Install our wrapper
|
||||
if (wrapper) {
|
||||
@@ -155,7 +249,17 @@ async function run () {
|
||||
}
|
||||
|
||||
// Add to path
|
||||
core.addPath(pathToCLI);
|
||||
addPath(pathToCLI);
|
||||
|
||||
// Set up provider acceptance test environment variables
|
||||
if (providerAcceptanceTest) {
|
||||
const exeSuffix = platform().startsWith('win') ? '.exe' : '';
|
||||
const binaryName = wrapper ? `tofu-bin${exeSuffix}` : `tofu${exeSuffix}`;
|
||||
exportVariable('TF_ACC', '1');
|
||||
exportVariable('TF_ACC_PROVIDER_NAMESPACE', 'hashicorp');
|
||||
exportVariable('TF_ACC_PROVIDER_HOST', 'registry.opentofu.org');
|
||||
exportVariable('TF_ACC_TERRAFORM_PATH', join(pathToCLI, binaryName));
|
||||
}
|
||||
|
||||
// Add credentials to file if they are provided
|
||||
if (credentialsHostname && credentialsToken) {
|
||||
@@ -163,9 +267,9 @@ async function run () {
|
||||
}
|
||||
return release;
|
||||
} catch (error) {
|
||||
core.error(error);
|
||||
logError(getErrorMessage(error));
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = run;
|
||||
export default run;
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
import { getErrorMessage, getErrorDetail } from '../error-utils.js';
|
||||
|
||||
describe('error-utils', () => {
|
||||
describe('getErrorMessage', () => {
|
||||
it('returns message for standard Error', () => {
|
||||
expect(getErrorMessage(new Error('Something failed'))).toBe('Something failed');
|
||||
});
|
||||
|
||||
it('includes cause for Error with cause (Node 16+)', () => {
|
||||
const cause = new Error('Network timeout');
|
||||
const err = new Error('Download failed');
|
||||
err.cause = cause;
|
||||
expect(getErrorMessage(err)).toBe('Download failed: Network timeout');
|
||||
});
|
||||
|
||||
it('flattens AggregateError into a single message', () => {
|
||||
const err = new AggregateError(
|
||||
[new Error('ECONNREFUSED'), new Error('getaddrinfo ENOTFOUND')],
|
||||
'AggregateError'
|
||||
);
|
||||
const msg = getErrorMessage(err);
|
||||
expect(msg).toContain('ECONNREFUSED');
|
||||
expect(msg).toContain('getaddrinfo ENOTFOUND');
|
||||
expect(msg).not.toBe('AggregateError');
|
||||
});
|
||||
|
||||
it('returns fallback for AggregateError with empty errors', () => {
|
||||
const err = new AggregateError([], 'AggregateError');
|
||||
expect(getErrorMessage(err)).toBe('AggregateError (one or more operations failed)');
|
||||
});
|
||||
|
||||
it('coerces non-Error to string', () => {
|
||||
expect(getErrorMessage('oops')).toBe('oops');
|
||||
expect(getErrorMessage(123)).toBe('123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getErrorDetail', () => {
|
||||
it('includes stack for Error', () => {
|
||||
const err = new Error('test');
|
||||
const detail = getErrorDetail(err);
|
||||
expect(detail).toContain('test');
|
||||
expect(detail).toContain('Error');
|
||||
});
|
||||
|
||||
it('includes each nested error for AggregateError', () => {
|
||||
const err = new AggregateError(
|
||||
[new Error('A'), new Error('B')],
|
||||
'AggregateError'
|
||||
);
|
||||
const detail = getErrorDetail(err);
|
||||
expect(detail).toContain('A');
|
||||
expect(detail).toContain('B');
|
||||
expect(detail).toContain('2 error(s)');
|
||||
});
|
||||
});
|
||||
});
|
||||
+185
-206
@@ -1,219 +1,198 @@
|
||||
const pkg = require('../releases');
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
import { Release, getRelease } from '../releases.js';
|
||||
|
||||
describe('getRelease', () => {
|
||||
function mockFetchReleases () {
|
||||
const mockReleasesMeta = [{
|
||||
tag_name: 'v1.6.0-alpha2',
|
||||
assets: [{
|
||||
name: 'tofu_1.6.0-alpha2_386.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_386.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_386.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_386.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_386.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_386.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_amd64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_amd64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_amd64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_amd64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_amd64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_amd64.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_arm64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_arm64.rpm'
|
||||
}, {
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_darwin_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_darwin_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_darwin_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_freebsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_freebsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_freebsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_freebsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_freebsd_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_freebsd_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_linux_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_linux_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_openbsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_openbsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_openbsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_openbsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_SHA256SUMS',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_SHA256SUMS'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_SHA256SUMS.pem',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_SHA256SUMS.pem'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_SHA256SUMS.sig',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_SHA256SUMS.sig'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_solaris_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_solaris_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_windows_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_windows_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha2_windows_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha2/tofu_1.6.0-alpha2_windows_amd64.zip'
|
||||
}]
|
||||
}, {
|
||||
tag_name: 'v1.6.0-alpha1',
|
||||
assets: [{
|
||||
name: 'tofu_1.6.0-alpha1_386.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_386.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_386.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_386.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_386.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_386.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_amd64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_amd64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_amd64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_amd64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_amd64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_amd64.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm64.apk',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm64.apk'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm64.deb',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm64.deb'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_arm64.rpm',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_arm64.rpm'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_darwin_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_darwin_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_darwin_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_darwin_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_freebsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_freebsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_freebsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_freebsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_freebsd_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_freebsd_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_arm.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_arm.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_linux_arm64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_linux_arm64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_openbsd_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_openbsd_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_openbsd_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_openbsd_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_SHA256SUMS',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_SHA256SUMS'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_SHA256SUMS.pem',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_SHA256SUMS.pem'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_SHA256SUMS.sig',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_SHA256SUMS.sig'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_solaris_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_solaris_amd64.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_windows_386.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_windows_386.zip'
|
||||
}, {
|
||||
name: 'tofu_1.6.0-alpha1_windows_amd64.zip',
|
||||
browser_download_url: 'https://github.com/opentofu/opentofu/releases/download/v1.6.0-alpha1/tofu_1.6.0-alpha1_windows_amd64.zip'
|
||||
}]
|
||||
}];
|
||||
const mockReleasesMeta = [
|
||||
{
|
||||
id: 'v1.7.0-alpha2',
|
||||
files: [
|
||||
'tofu_1.7.0-alpha2_386.apk',
|
||||
'tofu_1.7.0-alpha2_386.deb',
|
||||
'tofu_1.7.0-alpha2_386.rpm',
|
||||
'tofu_1.7.0-alpha2_amd64.apk',
|
||||
'tofu_1.7.0-alpha2_amd64.deb',
|
||||
'tofu_1.7.0-alpha2_amd64.rpm',
|
||||
'tofu_1.7.0-alpha2_arm.apk',
|
||||
'tofu_1.7.0-alpha2_arm.deb',
|
||||
'tofu_1.7.0-alpha2_arm.rpm',
|
||||
'tofu_1.7.0-alpha2_arm64.apk',
|
||||
'tofu_1.7.0-alpha2_arm64.deb',
|
||||
'tofu_1.7.0-alpha2_arm64.rpm',
|
||||
'tofu_1.7.0-alpha2_darwin_arm64.zip',
|
||||
'tofu_1.7.0-alpha2_freebsd_386.zip',
|
||||
'tofu_1.7.0-alpha2_freebsd_amd64.zip',
|
||||
'tofu_1.7.0-alpha2_freebsd_arm.zip',
|
||||
'tofu_1.7.0-alpha2_linux_386.zip',
|
||||
'tofu_1.7.0-alpha2_linux_amd64.zip',
|
||||
'tofu_1.7.0-alpha2_linux_arm.zip',
|
||||
'tofu_1.7.0-alpha2_linux_arm64.zip',
|
||||
'tofu_1.7.0-alpha2_openbsd_386.zip',
|
||||
'tofu_1.7.0-alpha2_openbsd_amd64.zip',
|
||||
'tofu_1.7.0-alpha2_SHA256SUMS',
|
||||
'tofu_1.7.0-alpha2_SHA256SUMS.pem',
|
||||
'tofu_1.7.0-alpha2_SHA256SUMS.sig',
|
||||
'tofu_1.7.0-alpha2_solaris_amd64.zip',
|
||||
'tofu_1.7.0-alpha2_windows_386.zip',
|
||||
'tofu_1.7.0-alpha2_windows_amd64.zip'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 'v1.6.0',
|
||||
files: [
|
||||
'tofu_1.6.0_386.apk',
|
||||
'tofu_1.6.0_386.deb',
|
||||
'tofu_1.6.0_386.rpm',
|
||||
'tofu_1.6.0_amd64.apk',
|
||||
'tofu_1.6.0_amd64.deb',
|
||||
'tofu_1.6.0_amd64.rpm',
|
||||
'tofu_1.6.0_arm.apk',
|
||||
'tofu_1.6.0_arm.deb',
|
||||
'tofu_1.6.0_arm.rpm',
|
||||
'tofu_1.6.0_arm64.apk',
|
||||
'tofu_1.6.0_arm64.deb',
|
||||
'tofu_1.6.0_arm64.rpm',
|
||||
'tofu_1.6.0_darwin_arm64.zip',
|
||||
'tofu_1.6.0_freebsd_386.zip',
|
||||
'tofu_1.6.0_freebsd_amd64.zip',
|
||||
'tofu_1.6.0_freebsd_arm.zip',
|
||||
'tofu_1.6.0_linux_386.zip',
|
||||
'tofu_1.6.0_linux_amd64.zip',
|
||||
'tofu_1.6.0_linux_arm.zip',
|
||||
'tofu_1.6.0_linux_arm64.zip',
|
||||
'tofu_1.6.0_openbsd_386.zip',
|
||||
'tofu_1.6.0_openbsd_amd64.zip',
|
||||
'tofu_1.6.0_SHA256SUMS',
|
||||
'tofu_1.6.0_SHA256SUMS.pem',
|
||||
'tofu_1.6.0_SHA256SUMS.sig',
|
||||
'tofu_1.6.0_solaris_amd64.zip',
|
||||
'tofu_1.6.0_windows_386.zip',
|
||||
'tofu_1.6.0_windows_amd64.zip'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 'v1.6.0-alpha2',
|
||||
files: [
|
||||
'tofu_1.6.0-alpha2_386.apk',
|
||||
'tofu_1.6.0-alpha2_386.deb',
|
||||
'tofu_1.6.0-alpha2_386.rpm',
|
||||
'tofu_1.6.0-alpha2_amd64.apk',
|
||||
'tofu_1.6.0-alpha2_amd64.deb',
|
||||
'tofu_1.6.0-alpha2_amd64.rpm',
|
||||
'tofu_1.6.0-alpha2_arm.apk',
|
||||
'tofu_1.6.0-alpha2_arm.deb',
|
||||
'tofu_1.6.0-alpha2_arm.rpm',
|
||||
'tofu_1.6.0-alpha2_arm64.apk',
|
||||
'tofu_1.6.0-alpha2_arm64.deb',
|
||||
'tofu_1.6.0-alpha2_arm64.rpm',
|
||||
'tofu_1.6.0-alpha2_darwin_arm64.zip',
|
||||
'tofu_1.6.0-alpha2_freebsd_386.zip',
|
||||
'tofu_1.6.0-alpha2_freebsd_amd64.zip',
|
||||
'tofu_1.6.0-alpha2_freebsd_arm.zip',
|
||||
'tofu_1.6.0-alpha2_linux_386.zip',
|
||||
'tofu_1.6.0-alpha2_linux_amd64.zip',
|
||||
'tofu_1.6.0-alpha2_linux_arm.zip',
|
||||
'tofu_1.6.0-alpha2_linux_arm64.zip',
|
||||
'tofu_1.6.0-alpha2_openbsd_386.zip',
|
||||
'tofu_1.6.0-alpha2_openbsd_amd64.zip',
|
||||
'tofu_1.6.0-alpha2_SHA256SUMS',
|
||||
'tofu_1.6.0-alpha2_SHA256SUMS.pem',
|
||||
'tofu_1.6.0-alpha2_SHA256SUMS.sig',
|
||||
'tofu_1.6.0-alpha2_solaris_amd64.zip',
|
||||
'tofu_1.6.0-alpha2_windows_386.zip',
|
||||
'tofu_1.6.0-alpha2_windows_amd64.zip'
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 'v1.6.0-alpha1',
|
||||
files: [
|
||||
'tofu_1.6.0-alpha1_386.apk',
|
||||
'tofu_1.6.0-alpha1_386.deb',
|
||||
'tofu_1.6.0-alpha1_386.rpm',
|
||||
'tofu_1.6.0-alpha1_amd64.apk',
|
||||
'tofu_1.6.0-alpha1_amd64.deb',
|
||||
'tofu_1.6.0-alpha1_amd64.rpm',
|
||||
'tofu_1.6.0-alpha1_arm.apk',
|
||||
'tofu_1.6.0-alpha1_arm.deb',
|
||||
'tofu_1.6.0-alpha1_arm.rpm',
|
||||
'tofu_1.6.0-alpha1_arm64.apk',
|
||||
'tofu_1.6.0-alpha1_arm64.deb',
|
||||
'tofu_1.6.0-alpha1_arm64.rpm',
|
||||
'tofu_1.6.0-alpha1_darwin_amd64.zip',
|
||||
'tofu_1.6.0-alpha1_darwin_arm64.zip',
|
||||
'tofu_1.6.0-alpha1_freebsd_386.zip',
|
||||
'tofu_1.6.0-alpha1_freebsd_amd64.zip',
|
||||
'tofu_1.6.0-alpha1_freebsd_arm.zip',
|
||||
'tofu_1.6.0-alpha1_linux_386.zip',
|
||||
'tofu_1.6.0-alpha1_linux_amd64.zip',
|
||||
'tofu_1.6.0-alpha1_linux_arm.zip',
|
||||
'tofu_1.6.0-alpha1_linux_arm64.zip',
|
||||
'tofu_1.6.0-alpha1_openbsd_386.zip',
|
||||
'tofu_1.6.0-alpha1_openbsd_amd64.zip',
|
||||
'tofu_1.6.0-alpha1_SHA256SUMS',
|
||||
'tofu_1.6.0-alpha1_SHA256SUMS.pem',
|
||||
'tofu_1.6.0-alpha1_SHA256SUMS.sig',
|
||||
'tofu_1.6.0-alpha1_solaris_amd64.zip',
|
||||
'tofu_1.6.0-alpha1_windows_386.zip',
|
||||
'tofu_1.6.0-alpha1_windows_amd64.zip'
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
return mockReleasesMeta.map(el => new pkg.Release(el));
|
||||
return mockReleasesMeta.map((el) => new Release(el));
|
||||
}
|
||||
|
||||
it.each(
|
||||
[
|
||||
['latest', '1.6.0-alpha2'],
|
||||
['<1.6.0-beta', '1.6.0-alpha2'],
|
||||
['>1.6.0-alpha1', '1.6.0-alpha2'],
|
||||
['>1.6.0-alpha1 <1.6.0', '1.6.0-alpha2'],
|
||||
['~1.6.0-alpha', '1.6.0-alpha2'],
|
||||
['<=1.6.0-alpha1', '1.6.0-alpha1']
|
||||
]
|
||||
)('happy path: getRelease(\'%s\') -> \'%s\'', async (input, wantVersion) => {
|
||||
const want = mockFetchReleases().find(el => el.version === wantVersion);
|
||||
const gotRelease = await pkg.getRelease(input, mockFetchReleases);
|
||||
it.each([
|
||||
['latest', '1.6.0'],
|
||||
['<1.6.0-beta', '1.6.0-alpha2'],
|
||||
['>1.6.0-alpha1', '1.6.0'],
|
||||
['>1.6.0-alpha1 <1.6.0', '1.6.0-alpha2'],
|
||||
['~1.6.0-alpha', '1.6.0'],
|
||||
['<=1.6.0-alpha1', '1.6.0-alpha1'],
|
||||
['>1.5.0', '1.6.0'],
|
||||
['>1.5.0', '1.6.0'],
|
||||
['>1.7.0-alpha', '1.7.0-alpha2']
|
||||
])("happy path: getRelease('%s') -> '%s'", async (input, wantVersion) => {
|
||||
const want = mockFetchReleases().find((el) => el.version === wantVersion);
|
||||
const gotRelease = await getRelease(input, '', mockFetchReleases);
|
||||
expect(gotRelease).toEqual(want);
|
||||
});
|
||||
|
||||
it.each(
|
||||
it.each([
|
||||
[
|
||||
['foo', 'Input version cannot be used, see semver: https://semver.org/spec/v2.0.0.html', undefined],
|
||||
['2.0', 'No matching version found', undefined],
|
||||
['latest', 'No tofu releases found, please contact OpenTofu', async () => { return null; }],
|
||||
['latest', 'No tofu releases found, please contact OpenTofu', async () => { return []; }]
|
||||
'foo',
|
||||
'Input version cannot be used, see semver: https://semver.org/spec/v2.0.0.html',
|
||||
mockFetchReleases
|
||||
],
|
||||
['2.0', 'No matching version found', mockFetchReleases],
|
||||
[
|
||||
'latest',
|
||||
'No tofu releases found, please contact OpenTofu',
|
||||
async () => {
|
||||
return null;
|
||||
}
|
||||
],
|
||||
[
|
||||
'latest',
|
||||
'No tofu releases found, please contact OpenTofu',
|
||||
async () => {
|
||||
return [];
|
||||
}
|
||||
]
|
||||
)('unhappy path: getRelease(\'%s\') -> throw Error(\'%s\')', async (input, wantErrorMessage, mockFetchReleasesFn) => {
|
||||
try {
|
||||
await pkg.getRelease(input, mockFetchReleasesFn);
|
||||
expect(true).toBe(false);
|
||||
} catch (e) {
|
||||
expect(e.message).toBe(wantErrorMessage);
|
||||
])(
|
||||
"unhappy path: getRelease('%s') -> throw Error('%s')",
|
||||
async (input, wantErrorMessage, mockFetchReleasesFn) => {
|
||||
try {
|
||||
await getRelease(input, '', mockFetchReleasesFn);
|
||||
expect(true).toBe(false);
|
||||
} catch (e) {
|
||||
expect(e.message).toBe(wantErrorMessage);
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,426 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
import { jest } from '@jest/globals';
|
||||
|
||||
// Node.js core
|
||||
import { promises as fs } from 'fs';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
|
||||
// Set up mocks BEFORE dynamic imports
|
||||
jest.unstable_mockModule('@actions/core', () => ({
|
||||
getInput: jest.fn(),
|
||||
getMultilineInput: jest.fn(),
|
||||
setOutput: jest.fn(),
|
||||
setFailed: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
error: jest.fn(),
|
||||
warning: jest.fn(),
|
||||
addPath: jest.fn(),
|
||||
exportVariable: jest.fn()
|
||||
}));
|
||||
|
||||
jest.unstable_mockModule('@actions/io', () => ({
|
||||
mv: jest.fn(),
|
||||
cp: jest.fn(),
|
||||
mkdirP: jest.fn()
|
||||
}));
|
||||
|
||||
jest.unstable_mockModule('@actions/tool-cache', () => ({
|
||||
downloadTool: jest.fn(),
|
||||
extractZip: jest.fn(),
|
||||
find: jest.fn(),
|
||||
cacheDir: jest.fn()
|
||||
}));
|
||||
|
||||
jest.unstable_mockModule('../releases.js', () => ({
|
||||
getRelease: jest.fn(),
|
||||
Release: jest.fn()
|
||||
}));
|
||||
|
||||
jest.unstable_mockModule('../util.js', () => ({
|
||||
fileSHA256: jest.fn()
|
||||
}));
|
||||
|
||||
// Dynamic imports pick up the mocks
|
||||
const { getInput, warning, exportVariable, getMultilineInput } = await import('@actions/core');
|
||||
const tc = await import('@actions/tool-cache');
|
||||
const io = await import('@actions/io');
|
||||
const { getRelease } = await import('../releases.js');
|
||||
const { default: setup } = await import('../setup-tofu.js');
|
||||
const { fileSHA256 } = await import('../util.js');
|
||||
|
||||
// Set up global test fixtures
|
||||
const fallbackVersion = 'latest';
|
||||
let tempDir;
|
||||
let tempDirPath;
|
||||
let version = '1.10.5';
|
||||
let versionFile;
|
||||
let versionFileName = '.opentofu-version';
|
||||
|
||||
describe('setup-tofu', () => {
|
||||
beforeAll(async () => {
|
||||
// Mock dependencies
|
||||
tc.downloadTool.mockResolvedValue('/mock/download/path');
|
||||
tc.extractZip.mockResolvedValue('/mock/extract/path');
|
||||
tc.find.mockReturnValue(null); // Default to cache miss
|
||||
tc.cacheDir.mockResolvedValue('/mock/cached/path');
|
||||
|
||||
io.mv.mockResolvedValue();
|
||||
io.cp.mockResolvedValue();
|
||||
io.mkdirP.mockResolvedValue();
|
||||
|
||||
const mockRelease = {
|
||||
version: '1.10.5',
|
||||
getBuild: jest.fn().mockReturnValue({ url: 'mock-url' })
|
||||
};
|
||||
getRelease.mockResolvedValue(mockRelease);
|
||||
|
||||
// Write version file to temporary directory
|
||||
tempDirPath = join(tmpdir(), 'setup-tofu-');
|
||||
tempDir = await fs.mkdtemp(tempDirPath);
|
||||
versionFile = join(tempDir, versionFileName);
|
||||
await fs.writeFile(versionFile, `${version}\n`);
|
||||
|
||||
// Mock action inputs to return default values
|
||||
getInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
tofu_version: fallbackVersion,
|
||||
tofu_version_file: versionFile,
|
||||
cli_config_credentials_hostname: '',
|
||||
cli_config_credentials_token: '',
|
||||
tofu_wrapper: 'true',
|
||||
provider_acceptance_tests: 'false',
|
||||
cache: 'false',
|
||||
github_token: ''
|
||||
};
|
||||
return defaults[name] || '';
|
||||
});
|
||||
|
||||
getMultilineInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
checksums: []
|
||||
};
|
||||
return defaults[name] || [];
|
||||
});
|
||||
|
||||
// Mock environment variables
|
||||
process.env.GITHUB_TOKEN = 'mock-github-token';
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
delete process.env.GITHUB_TOKEN;
|
||||
});
|
||||
|
||||
describe('tofu_version_file functionality', () => {
|
||||
it('should read version from file when tofu_version_file is provided', async () => {
|
||||
jest.spyOn(fs, 'readFile');
|
||||
|
||||
await setup();
|
||||
|
||||
expect(getRelease).toHaveBeenCalledWith(
|
||||
version,
|
||||
process.env.GITHUB_TOKEN
|
||||
);
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle empty version file gracefully', async () => {
|
||||
jest.spyOn(fs, 'readFile');
|
||||
|
||||
version = ' ';
|
||||
versionFileName = '.opentofu-version-empty';
|
||||
versionFile = join(tempDir, versionFileName);
|
||||
await fs.writeFile(versionFile, `${version}\n`);
|
||||
|
||||
getInput.mockImplementation((name) => {
|
||||
if (name === 'tofu_version_file') {
|
||||
return versionFile;
|
||||
}
|
||||
if (name === 'tofu_version') {
|
||||
return fallbackVersion;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(getRelease).toHaveBeenCalledWith(
|
||||
fallbackVersion,
|
||||
process.env.GITHUB_TOKEN
|
||||
);
|
||||
|
||||
expect(warning).toHaveBeenCalledWith(
|
||||
expect.stringContaining(`Version file ${versionFile} is empty`)
|
||||
);
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle file read errors gracefully', async () => {
|
||||
jest.spyOn(fs, 'readFile');
|
||||
|
||||
versionFileName = '.opentofu-version-file-does-not-exist';
|
||||
versionFile = join(tempDir, versionFileName);
|
||||
|
||||
getInput.mockImplementation((name) => {
|
||||
if (name === 'tofu_version_file') {
|
||||
return versionFile;
|
||||
}
|
||||
if (name === 'tofu_version') {
|
||||
return fallbackVersion;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(getRelease).toHaveBeenCalledWith(
|
||||
fallbackVersion,
|
||||
process.env.GITHUB_TOKEN
|
||||
);
|
||||
|
||||
expect(warning).toHaveBeenCalledWith(
|
||||
expect.stringContaining(
|
||||
`Failed to read version from file ${versionFile}`
|
||||
)
|
||||
);
|
||||
|
||||
expect(fs.readFile).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not read file when tofu_version_file is not provided', async () => {
|
||||
jest.spyOn(fs, 'readFile');
|
||||
|
||||
getInput.mockImplementation((name) => {
|
||||
if (name === 'tofu_version') {
|
||||
return fallbackVersion;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(getRelease).toHaveBeenCalledWith(
|
||||
fallbackVersion,
|
||||
process.env.GITHUB_TOKEN
|
||||
);
|
||||
|
||||
expect(fs.readFile).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('caching functionality', () => {
|
||||
it('should use cached version when cache is enabled and found', async () => {
|
||||
tc.find.mockReturnValue('/mock/cached/path');
|
||||
|
||||
getInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
tofu_version: fallbackVersion,
|
||||
tofu_version_file: '',
|
||||
cli_config_credentials_hostname: '',
|
||||
cli_config_credentials_token: '',
|
||||
tofu_wrapper: 'true',
|
||||
cache: 'true',
|
||||
github_token: ''
|
||||
};
|
||||
return defaults[name] || '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(tc.find).toHaveBeenCalledWith('tofu', '1.10.5', expect.any(String));
|
||||
expect(tc.downloadTool).not.toHaveBeenCalled();
|
||||
expect(tc.extractZip).not.toHaveBeenCalled();
|
||||
expect(tc.cacheDir).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should download and cache when cache is enabled but not found', async () => {
|
||||
tc.find.mockReturnValue(null); // Cache miss
|
||||
|
||||
getInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
tofu_version: fallbackVersion,
|
||||
tofu_version_file: '',
|
||||
cli_config_credentials_hostname: '',
|
||||
cli_config_credentials_token: '',
|
||||
tofu_wrapper: 'true',
|
||||
cache: 'true',
|
||||
github_token: ''
|
||||
};
|
||||
return defaults[name] || '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(tc.find).toHaveBeenCalledWith('tofu', '1.10.5', expect.any(String));
|
||||
expect(tc.downloadTool).toHaveBeenCalled();
|
||||
expect(tc.extractZip).toHaveBeenCalled();
|
||||
expect(tc.cacheDir).toHaveBeenCalledWith('/mock/extract/path', 'tofu', '1.10.5', expect.any(String));
|
||||
});
|
||||
|
||||
it('should not use cache when cache is disabled', async () => {
|
||||
tc.find.mockReturnValue('/mock/cached/path');
|
||||
|
||||
getInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
tofu_version: fallbackVersion,
|
||||
tofu_version_file: '',
|
||||
cli_config_credentials_hostname: '',
|
||||
cli_config_credentials_token: '',
|
||||
tofu_wrapper: 'true',
|
||||
provider_acceptance_tests: 'false',
|
||||
cache: 'false',
|
||||
github_token: ''
|
||||
};
|
||||
return defaults[name] || '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(tc.find).not.toHaveBeenCalled();
|
||||
expect(tc.downloadTool).toHaveBeenCalled();
|
||||
expect(tc.extractZip).toHaveBeenCalled();
|
||||
expect(tc.cacheDir).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('provider_acceptance_tests functionality', () => {
|
||||
it('should export acceptance test env vars when enabled without wrapper', async () => {
|
||||
getInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
tofu_version: fallbackVersion,
|
||||
tofu_version_file: '',
|
||||
cli_config_credentials_hostname: '',
|
||||
cli_config_credentials_token: '',
|
||||
tofu_wrapper: 'false',
|
||||
provider_acceptance_tests: 'true',
|
||||
cache: 'false',
|
||||
github_token: ''
|
||||
};
|
||||
return defaults[name] || '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(exportVariable).toHaveBeenCalledWith('TF_ACC', '1');
|
||||
expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_NAMESPACE', 'hashicorp');
|
||||
expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_HOST', 'registry.opentofu.org');
|
||||
expect(exportVariable).toHaveBeenCalledWith(
|
||||
'TF_ACC_TERRAFORM_PATH',
|
||||
expect.stringContaining('tofu')
|
||||
);
|
||||
expect(exportVariable).not.toHaveBeenCalledWith(
|
||||
'TF_ACC_TERRAFORM_PATH',
|
||||
expect.stringContaining('tofu-bin')
|
||||
);
|
||||
});
|
||||
|
||||
it('should point TF_ACC_TERRAFORM_PATH to tofu-bin when wrapper is enabled', async () => {
|
||||
getInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
tofu_version: fallbackVersion,
|
||||
tofu_version_file: '',
|
||||
cli_config_credentials_hostname: '',
|
||||
cli_config_credentials_token: '',
|
||||
tofu_wrapper: 'true',
|
||||
provider_acceptance_tests: 'true',
|
||||
cache: 'false',
|
||||
github_token: ''
|
||||
};
|
||||
return defaults[name] || '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(exportVariable).toHaveBeenCalledWith('TF_ACC', '1');
|
||||
expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_NAMESPACE', 'hashicorp');
|
||||
expect(exportVariable).toHaveBeenCalledWith('TF_ACC_PROVIDER_HOST', 'registry.opentofu.org');
|
||||
expect(exportVariable).toHaveBeenCalledWith(
|
||||
'TF_ACC_TERRAFORM_PATH',
|
||||
expect.stringContaining('tofu-bin')
|
||||
);
|
||||
});
|
||||
|
||||
it('should not export acceptance test env vars when disabled', async () => {
|
||||
getInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
tofu_version: fallbackVersion,
|
||||
tofu_version_file: '',
|
||||
cli_config_credentials_hostname: '',
|
||||
cli_config_credentials_token: '',
|
||||
tofu_wrapper: 'false',
|
||||
provider_acceptance_tests: 'false',
|
||||
cache: 'false',
|
||||
github_token: ''
|
||||
};
|
||||
return defaults[name] || '';
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC', expect.anything());
|
||||
expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC_PROVIDER_NAMESPACE', expect.anything());
|
||||
expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC_PROVIDER_HOST', expect.anything());
|
||||
expect(exportVariable).not.toHaveBeenCalledWith('TF_ACC_TERRAFORM_PATH', expect.anything());
|
||||
});
|
||||
});
|
||||
|
||||
describe('checksum validation', () => {
|
||||
const mockChecksum = 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6';
|
||||
const mockChecksum2 = 'fcfac0a865b57e0711cb8bae47c8fc70ad93198fa6146a40020ea459715fda83';
|
||||
const mockInvalidChecksum = 'invalidchecksuminvalidchecksuminvalidchecksuminvalidchecksum';
|
||||
fileSHA256.mockResolvedValue(mockChecksum);
|
||||
|
||||
it('should validate checksum when checksums are provided', async () => {
|
||||
getMultilineInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
checksums: [mockInvalidChecksum, mockChecksum]
|
||||
|
||||
};
|
||||
return defaults[name] ?? [];
|
||||
});
|
||||
|
||||
await setup();
|
||||
expect(fileSHA256).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not validate checksum when no checksums are provided', async () => {
|
||||
getMultilineInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
checksums: []
|
||||
|
||||
};
|
||||
return defaults[name] ?? [];
|
||||
});
|
||||
|
||||
await setup();
|
||||
|
||||
expect(fileSHA256).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error when checksum validation fails', async () => {
|
||||
fileSHA256.mockResolvedValue(mockInvalidChecksum);
|
||||
|
||||
getMultilineInput.mockImplementation((name) => {
|
||||
const defaults = {
|
||||
checksums: [mockChecksum, mockChecksum2]
|
||||
|
||||
};
|
||||
return defaults[name] ?? [];
|
||||
});
|
||||
await expect(setup()).rejects.toMatchObject(new Error(
|
||||
`Failed to validate OpenTofu CLI zip checksum. Received: ${mockInvalidChecksum}. Valid checksums: ${mockChecksum}, ${mockChecksum2}`));
|
||||
expect(fileSHA256).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
import { createHash } from 'crypto';
|
||||
import { createReadStream } from 'fs';
|
||||
import { pipeline } from 'stream/promises';
|
||||
|
||||
export async function fileSHA256 (filePath) {
|
||||
const hash = createHash('sha256');
|
||||
const fileStream = createReadStream(filePath);
|
||||
|
||||
await pipeline(fileStream, hash);
|
||||
return hash.digest('hex');
|
||||
}
|
||||
Generated
+7082
-6367
File diff suppressed because it is too large.
Load diff
+39
-38
@@ -1,40 +1,41 @@
|
||||
{
|
||||
"name": "setup-opentofu",
|
||||
"version": "0.0.1",
|
||||
"description": "Setup OpenTofu CLI for GitHub Actions",
|
||||
"license": "MPL-2.0",
|
||||
"publisher": "OpenTofu",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/opentofu/setup-opentofu.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "semistandard --env jest && jest --coverage",
|
||||
"lint": "semistandard --env jest --fix",
|
||||
"build": "ncc build wrapper/tofu.js --out wrapper/dist && ncc build index.js --out dist",
|
||||
"prepare": "husky install",
|
||||
"format-check": "echo \"unimplemented for actions/reusable-workflows basic-validation\""
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"dependencies": {
|
||||
"@actions/core": "1.10.1",
|
||||
"@actions/exec": "1.1.1",
|
||||
"@actions/io": "1.1.3",
|
||||
"@actions/tool-cache": "2.0.1",
|
||||
"semver": "7.5.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vercel/ncc": "0.38.0",
|
||||
"husky": "8.0.3",
|
||||
"jest": "29.7.0",
|
||||
"nock": "13.3.3",
|
||||
"semistandard": "17.0.0"
|
||||
},
|
||||
"semistandard": {
|
||||
"ignore": [
|
||||
"**/dist/**"
|
||||
]
|
||||
}
|
||||
"name": "setup-opentofu",
|
||||
"version": "1.0.1",
|
||||
"description": "Setup OpenTofu CLI for GitHub Actions",
|
||||
"type": "module",
|
||||
"license": "MPL-2.0",
|
||||
"publisher": "OpenTofu",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/opentofu/setup-opentofu.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "semistandard --env jest && node --experimental-vm-modules ./node_modules/jest/bin/jest.js --coverage",
|
||||
"lint": "semistandard --env jest --fix",
|
||||
"build": "ncc build wrapper/tofu.js --out wrapper/dist && ncc build index.js --out dist",
|
||||
"prepare": "husky install",
|
||||
"format-check": "echo \"unimplemented for actions/reusable-workflows basic-validation\""
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"dependencies": {
|
||||
"@actions/core": "^3.0.0",
|
||||
"@actions/exec": "^3.0.0",
|
||||
"@actions/http-client": "4.0.1",
|
||||
"@actions/io": "^3.0.2",
|
||||
"@actions/tool-cache": "^4.0.0",
|
||||
"semver": "^7.7.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vercel/ncc": "0.38.4",
|
||||
"husky": "9.1.7",
|
||||
"jest": "^30.3.0",
|
||||
"semistandard": "17.0.0"
|
||||
},
|
||||
"semistandard": {
|
||||
"ignore": [
|
||||
"**/dist/**"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
@@ -9,7 +10,8 @@
|
||||
*
|
||||
* @example
|
||||
* // Instantiate a new listener
|
||||
* const listener = new OutputListener();
|
||||
* // stream is used to write the data before waiting for the listener to complete
|
||||
* const listener = new OutputListener(stream);
|
||||
* // Register listener against STDOUT stream
|
||||
* await exec.exec('ls', ['-ltr'], {
|
||||
* listeners: {
|
||||
@@ -19,23 +21,21 @@
|
||||
* // Log out STDOUT contents
|
||||
* console.log(listener.contents);
|
||||
*/
|
||||
class OutputListener {
|
||||
constructor () {
|
||||
export default class OutputListener {
|
||||
constructor (stream) {
|
||||
this._buff = [];
|
||||
this._stream = stream;
|
||||
}
|
||||
|
||||
get listener () {
|
||||
const listen = function listen (data) {
|
||||
this._stream.write(data);
|
||||
this._buff.push(data);
|
||||
};
|
||||
return listen.bind(this);
|
||||
}
|
||||
|
||||
get contents () {
|
||||
return this._buff
|
||||
.map(chunk => chunk.toString())
|
||||
.join('');
|
||||
return this._buff.map((chunk) => chunk.toString()).join('');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OutputListener;
|
||||
@@ -1,14 +1,17 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
|
||||
module.exports = (() => {
|
||||
const tofuBin = (() => {
|
||||
// If we're on Windows, then the executable ends with .exe
|
||||
const exeSuffix = os.platform().startsWith('win') ? '.exe' : '';
|
||||
|
||||
return [process.env.TOFU_CLI_PATH, `tofu-bin${exeSuffix}`].join(path.sep);
|
||||
})();
|
||||
|
||||
export default tofuBin;
|
||||
@@ -1,17 +1,23 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
const OutputListener = require('../lib/output-listener');
|
||||
import OutputListener from '../lib/output-listener.js';
|
||||
import { PassThrough } from 'stream';
|
||||
|
||||
describe('output-listener', () => {
|
||||
it('receives and exposes data', () => {
|
||||
const listener = new OutputListener();
|
||||
const stream = new PassThrough();
|
||||
const listener = new OutputListener(stream);
|
||||
const listen = listener.listener;
|
||||
listen(Buffer.from('foo'));
|
||||
expect(stream.read()).toEqual(Buffer.from('foo'));
|
||||
listen(Buffer.from('bar'));
|
||||
expect(stream.read()).toEqual(Buffer.from('bar'));
|
||||
listen(Buffer.from('baz'));
|
||||
expect(stream.read()).toEqual(Buffer.from('baz'));
|
||||
expect(listener.contents).toEqual('foobarbaz');
|
||||
});
|
||||
});
|
||||
+15
-17
@@ -1,20 +1,21 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* Copyright (c) OpenTofu
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
const io = require('@actions/io');
|
||||
const core = require('@actions/core');
|
||||
const { exec } = require('@actions/exec');
|
||||
import { which } from '@actions/io';
|
||||
import { setOutput, setFailed } from '@actions/core';
|
||||
import { exec } from '@actions/exec';
|
||||
|
||||
const OutputListener = require('./lib/output-listener');
|
||||
const pathToCLI = require('./lib/tofu-bin');
|
||||
import OutputListener from './lib/output-listener.js';
|
||||
import pathToCLI from './lib/tofu-bin.js';
|
||||
|
||||
async function checkTofu () {
|
||||
// Setting check to `true` will cause `which` to throw if tofu isn't found
|
||||
const check = true;
|
||||
return io.which(pathToCLI, check);
|
||||
return which(pathToCLI, check);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
@@ -22,8 +23,8 @@ async function checkTofu () {
|
||||
await checkTofu();
|
||||
|
||||
// Create listeners to receive output (in memory) as well
|
||||
const stdout = new OutputListener();
|
||||
const stderr = new OutputListener();
|
||||
const stdout = new OutputListener(process.stdout);
|
||||
const stderr = new OutputListener(process.stderr);
|
||||
const listeners = {
|
||||
stdout: stdout.listener,
|
||||
stderr: stderr.listener
|
||||
@@ -33,18 +34,15 @@ async function checkTofu () {
|
||||
const args = process.argv.slice(2);
|
||||
const options = {
|
||||
listeners,
|
||||
ignoreReturnCode: true
|
||||
ignoreReturnCode: true,
|
||||
silent: true // don't print "[command...]" into stdout: https://github.com/actions/toolkit/issues/649
|
||||
};
|
||||
const exitCode = await exec(pathToCLI, args, options);
|
||||
core.debug(`OpenTofu exited with code ${exitCode}.`);
|
||||
core.debug(`stdout: ${stdout.contents}`);
|
||||
core.debug(`stderr: ${stderr.contents}`);
|
||||
core.debug(`exitcode: ${exitCode}`);
|
||||
|
||||
// Set outputs, result, exitcode, and stderr
|
||||
core.setOutput('stdout', stdout.contents);
|
||||
core.setOutput('stderr', stderr.contents);
|
||||
core.setOutput('exitcode', exitCode.toString(10));
|
||||
setOutput('stdout', stdout.contents);
|
||||
setOutput('stderr', stderr.contents);
|
||||
setOutput('exitcode', exitCode.toString(10));
|
||||
|
||||
if (exitCode === 0 || exitCode === 2) {
|
||||
// A exitCode of 0 is considered a success
|
||||
@@ -55,5 +53,5 @@ async function checkTofu () {
|
||||
}
|
||||
|
||||
// A non-zero exitCode is considered an error
|
||||
core.setFailed(`OpenTofu exited with code ${exitCode}.`);
|
||||
setFailed(`OpenTofu exited with code ${exitCode}.`);
|
||||
})();
|
||||
Reference in new issue
Block a user