From b4be8cbf9dfeed652f96f0ba95317742f637d72b Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Thu, 17 Oct 2024 16:01:48 -0400 Subject: [PATCH 1/3] Add acceptance tests for `gh variable` --- .../testdata/variable/variable-org.txtar | 20 +++++++++++ .../testdata/variable/variable-repo-env.txtar | 33 +++++++++++++++++++ .../testdata/variable/variable-repo.txtar | 29 ++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 acceptance/testdata/variable/variable-org.txtar create mode 100644 acceptance/testdata/variable/variable-repo-env.txtar create mode 100644 acceptance/testdata/variable/variable-repo.txtar diff --git a/acceptance/testdata/variable/variable-org.txtar b/acceptance/testdata/variable/variable-org.txtar new file mode 100644 index 000000000..370e63b0d --- /dev/null +++ b/acceptance/testdata/variable/variable-org.txtar @@ -0,0 +1,20 @@ +# Prepare organization variable, GitHub Actions uppercases names +env2upper ORG_VARIABLE_NAME=$RANDOM_STRING + +# Confirm organization variable does not exist, will fail admin:org scope missing +! exec gh variable get $ORG_VARIABLE_NAME --org $ORG +stderr 'variable .+ was not found' + +# Create an organization variable +exec gh variable set $ORG_VARIABLE_NAME --org $ORG --body 'just an org variable' + +# Defer organization variable cleanup +defer gh variable delete $ORG_VARIABLE_NAME --org $ORG + +# Confirm organization variable exists +exec gh variable list --org $ORG +stdout $ORG_VARIABLE_NAME + +# Verify repository environment variable can be retrieved +exec gh variable get $ORG_VARIABLE_NAME --org $ORG +stdout 'just an org variable' diff --git a/acceptance/testdata/variable/variable-repo-env.txtar b/acceptance/testdata/variable/variable-repo-env.txtar new file mode 100644 index 000000000..f4b11c3dd --- /dev/null +++ b/acceptance/testdata/variable/variable-repo-env.txtar @@ -0,0 +1,33 @@ +# Force GitHub CLI to treat testscript as TTY +env GH_FORCE_TTY=80 +env REPO=$SCRIPT_NAME-$RANDOM_STRING + +# Create a repository where the variable will be registered +exec gh repo create $ORG/$REPO --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes $ORG/$REPO + +# Clone the repo +exec gh repo clone $ORG/$REPO +cd $REPO + +# Create a repository environment, will fail if organization does not have environment support +exec gh api /repos/$ORG/$REPO/environments/testscripts -X PUT --jq '.name' +stdout 'testscripts' + +# Verify no repository environment variables exist +exec gh variable list --env testscripts +stderr 'no variables found' + +# Create a repository environment variable +exec gh variable set TESTSCRIPTS_ENV --env testscripts --body 'just a repo env variable' +stdout 'Created variable TESTSCRIPTS_ENV for' + +# Verify new repository environment variable exists +exec gh variable list --env testscripts +stdout 'TESTSCRIPTS_ENV\s+just a repo env variable\s+less than a minute ago' + +# Verify repository environment variable can be retrieved +exec gh variable get TESTSCRIPTS_ENV --env testscripts +stdout 'just a repo env variable' diff --git a/acceptance/testdata/variable/variable-repo.txtar b/acceptance/testdata/variable/variable-repo.txtar new file mode 100644 index 000000000..f6295b566 --- /dev/null +++ b/acceptance/testdata/variable/variable-repo.txtar @@ -0,0 +1,29 @@ +# Setup environment variables to force TTY +env GH_FORCE_TTY=80 +env REPO=$SCRIPT_NAME-$RANDOM_STRING + +# Create a repository where the variable will be registered +exec gh repo create $ORG/$REPO --add-readme --private + +# Defer repo cleanup +defer gh repo delete --yes $ORG/$REPO + +# Clone the repo +exec gh repo clone $ORG/$REPO +cd $REPO + +# Verify no repository variables exist +exec gh variable list +stderr 'no variables found' + +# Create a repository variable +exec gh variable set TESTSCRIPTS --body 'just a repo variable' +stdout 'Created variable TESTSCRIPTS for' + +# Verify new repository variable exists +exec gh variable list +stdout 'TESTSCRIPTS\s+just a repo variable\s+less than a minute ago' + +# Verify repository variable can be retrieved +exec gh variable get TESTSCRIPTS +stdout 'just a repo variable' From e8842f861fb54570a0bcaf2178eaa172653743f9 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Thu, 17 Oct 2024 16:03:15 -0400 Subject: [PATCH 2/3] Add missing test to trigger acceptance tests --- acceptance/acceptance_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index 35ea7b40b..ef8e85ddc 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -52,6 +52,15 @@ func TestSecrets(t *testing.T) { testscript.Run(t, testScriptParamsFor(tsEnv, "secret")) } +func TestVariables(t *testing.T) { + var tsEnv testScriptEnv + if err := tsEnv.fromEnv(); err != nil { + t.Fatal(err) + } + + testscript.Run(t, testScriptParamsFor(tsEnv, "variable")) +} + func testScriptParamsFor(tsEnv testScriptEnv, command string) testscript.Params { var files []string if tsEnv.script != "" { From 6192ac74df5a583fb487f3f248064738e9fa9b32 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 21 Oct 2024 16:02:26 -0400 Subject: [PATCH 3/3] Update variable testscripts based on secret This commit refactors a bit of the variable testscripts given the changes in the secret testscripts efforts, removing potentially unnecessary assertions and making these look more similar to other testscripts. --- .../testdata/variable/variable-org.txtar | 20 +++++++-------- .../testdata/variable/variable-repo-env.txtar | 25 +++++++++---------- .../testdata/variable/variable-repo.txtar | 17 ++++++------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/acceptance/testdata/variable/variable-org.txtar b/acceptance/testdata/variable/variable-org.txtar index 370e63b0d..c09381b29 100644 --- a/acceptance/testdata/variable/variable-org.txtar +++ b/acceptance/testdata/variable/variable-org.txtar @@ -1,20 +1,20 @@ -# Prepare organization variable, GitHub Actions uppercases names -env2upper ORG_VARIABLE_NAME=$RANDOM_STRING +# Setup environment variables used for testscript +env2upper VAR_NAME=${SCRIPT_NAME}_${RANDOM_STRING} # Confirm organization variable does not exist, will fail admin:org scope missing -! exec gh variable get $ORG_VARIABLE_NAME --org $ORG -stderr 'variable .+ was not found' +exec gh variable list --org $ORG +! stdout $VAR_NAME # Create an organization variable -exec gh variable set $ORG_VARIABLE_NAME --org $ORG --body 'just an org variable' +exec gh variable set $VAR_NAME --org $ORG --body 'just an org variable' # Defer organization variable cleanup -defer gh variable delete $ORG_VARIABLE_NAME --org $ORG +defer gh variable delete $VAR_NAME --org $ORG -# Confirm organization variable exists +# Verify new organization variable exists exec gh variable list --org $ORG -stdout $ORG_VARIABLE_NAME +stdout $VAR_NAME -# Verify repository environment variable can be retrieved -exec gh variable get $ORG_VARIABLE_NAME --org $ORG +# Verify organization variable can be retrieved +exec gh variable get $VAR_NAME --org $ORG stdout 'just an org variable' diff --git a/acceptance/testdata/variable/variable-repo-env.txtar b/acceptance/testdata/variable/variable-repo-env.txtar index f4b11c3dd..99dbb6b9f 100644 --- a/acceptance/testdata/variable/variable-repo-env.txtar +++ b/acceptance/testdata/variable/variable-repo-env.txtar @@ -1,6 +1,7 @@ -# Force GitHub CLI to treat testscript as TTY -env GH_FORCE_TTY=80 -env REPO=$SCRIPT_NAME-$RANDOM_STRING +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} +env ENV_NAME=testscripts +env VAR_NAME=TESTSCRIPTS_ENV # Create a repository where the variable will be registered exec gh repo create $ORG/$REPO --add-readme --private @@ -13,21 +14,19 @@ exec gh repo clone $ORG/$REPO cd $REPO # Create a repository environment, will fail if organization does not have environment support -exec gh api /repos/$ORG/$REPO/environments/testscripts -X PUT --jq '.name' -stdout 'testscripts' +exec gh api /repos/$ORG/$REPO/environments/$ENV_NAME -X PUT --jq '.name' -# Verify no repository environment variables exist -exec gh variable list --env testscripts -stderr 'no variables found' +# Verify repository environment variable does not exist +exec gh variable list --env $ENV_NAME +! stdout $VAR_NAME # Create a repository environment variable -exec gh variable set TESTSCRIPTS_ENV --env testscripts --body 'just a repo env variable' -stdout 'Created variable TESTSCRIPTS_ENV for' +exec gh variable set $VAR_NAME --env $ENV_NAME --body 'just a repo env variable' # Verify new repository environment variable exists -exec gh variable list --env testscripts -stdout 'TESTSCRIPTS_ENV\s+just a repo env variable\s+less than a minute ago' +exec gh variable list --env $ENV_NAME +stdout $VAR_NAME # Verify repository environment variable can be retrieved -exec gh variable get TESTSCRIPTS_ENV --env testscripts +exec gh variable get $VAR_NAME --env $ENV_NAME stdout 'just a repo env variable' diff --git a/acceptance/testdata/variable/variable-repo.txtar b/acceptance/testdata/variable/variable-repo.txtar index f6295b566..9ff64db0f 100644 --- a/acceptance/testdata/variable/variable-repo.txtar +++ b/acceptance/testdata/variable/variable-repo.txtar @@ -1,6 +1,6 @@ -# Setup environment variables to force TTY -env GH_FORCE_TTY=80 -env REPO=$SCRIPT_NAME-$RANDOM_STRING +# Setup environment variables used for testscript +env REPO=${SCRIPT_NAME}-${RANDOM_STRING} +env VAR_NAME=TESTSCRIPTS # Create a repository where the variable will be registered exec gh repo create $ORG/$REPO --add-readme --private @@ -12,18 +12,17 @@ defer gh repo delete --yes $ORG/$REPO exec gh repo clone $ORG/$REPO cd $REPO -# Verify no repository variables exist +# Verify repository variable does not exist exec gh variable list -stderr 'no variables found' +! stdout $VAR_NAME # Create a repository variable -exec gh variable set TESTSCRIPTS --body 'just a repo variable' -stdout 'Created variable TESTSCRIPTS for' +exec gh variable set $VAR_NAME --body 'just a repo variable' # Verify new repository variable exists exec gh variable list -stdout 'TESTSCRIPTS\s+just a repo variable\s+less than a minute ago' +stdout $VAR_NAME # Verify repository variable can be retrieved -exec gh variable get TESTSCRIPTS +exec gh variable get $VAR_NAME stdout 'just a repo variable'