Skip to content

Commit

Permalink
Fix creating a project variable without a selected env
Browse files Browse the repository at this point in the history
Also adds tests for creating and updating variables
  • Loading branch information
pjcdawkins committed Dec 27, 2024
1 parent 952e914 commit 06f5770
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/platformsh/legacy-cli/tests
go 1.22.9

require (
github.com/platformsh/cli v0.0.0-20241225001542-c2a7282c12d5
github.com/platformsh/cli v0.0.0-20241227091635-fea73d95f802
github.com/stretchr/testify v1.9.0
)

Expand Down
4 changes: 2 additions & 2 deletions go-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/platformsh/cli v0.0.0-20241225001542-c2a7282c12d5 h1:k3nAT1td76kAXtyriBFkP9eYsFl7PFRk6p1zIKJiwXI=
github.com/platformsh/cli v0.0.0-20241225001542-c2a7282c12d5/go.mod h1:92H+miZV5F6iXLr/2698VLSZI8X7h+l/xd+i+1JaBAY=
github.com/platformsh/cli v0.0.0-20241227091635-fea73d95f802 h1:gkfWMyJ5lVWD3RUY878iiOaltaCUgPwUstLqVo4hmUc=
github.com/platformsh/cli v0.0.0-20241227091635-fea73d95f802/go.mod h1:92H+miZV5F6iXLr/2698VLSZI8X7h+l/xd+i+1JaBAY=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
Expand Down
File renamed without changes.
72 changes: 72 additions & 0 deletions go-tests/variable_write_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package tests

import (
"net/http/httptest"
"testing"

"github.com/platformsh/cli/pkg/mockapi"
"github.com/stretchr/testify/assert"
)

func TestVariableCreate(t *testing.T) {
authServer := mockapi.NewAuthServer(t)
defer authServer.Close()

apiHandler := mockapi.NewHandler(t)
apiServer := httptest.NewServer(apiHandler)
defer apiServer.Close()

projectID := "ohnguk9zeiw3b"

apiHandler.SetProjects([]*mockapi.Project{{
ID: projectID,
Links: mockapi.MakeHALLinks("self=/projects/"+projectID,
"environments=/projects/"+projectID+"/environments"),
DefaultBranch: "main",
}})
main := makeEnv(projectID, "main", "production", "active", nil)
main.Links["#variables"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main/variables"}
main.Links["#manage-variables"] = mockapi.HALLink{HREF: "/projects/" + projectID + "/environments/main/variables"}
envs := []*mockapi.Environment{main}
apiHandler.SetEnvironments(envs)

apiHandler.SetProjectVariables(projectID, []*mockapi.Variable{
{
Name: "bar",
IsSensitive: true,
VisibleBuild: true,
},
})

f := newCommandFactory(t, apiServer.URL, authServer.URL)

_, stdErr, err := f.RunCombinedOutput("var:create", "-p", projectID, "-l", "e", "-e", "main", "env:TEST", "--value", "env-level-value")
assert.NoError(t, err)
assert.Contains(t, stdErr, "Creating variable env:TEST on the environment main")

assertTrimmed(t, "env-level-value", f.Run("var:get", "-p", projectID, "-e", "main", "env:TEST", "-P", "value"))

_, stdErr, err = f.RunCombinedOutput("var:create", "-p", projectID, "env:TEST", "-l", "p", "--value", "project-level-value")
assert.NoError(t, err)
assert.Contains(t, stdErr, "Creating variable env:TEST on the project "+projectID)

assertTrimmed(t, "project-level-value", f.Run("var:get", "-p", projectID, "-e", "main", "env:TEST", "-P", "value", "-l", "p"))
assertTrimmed(t, "env-level-value", f.Run("var:get", "-p", projectID, "-e", "main", "env:TEST", "-P", "value", "-l", "e"))

_, stdErr, err = f.RunCombinedOutput("var:create", "-p", projectID, "bar", "-l", "p", "--value", "test")
assert.Error(t, err)
assert.Contains(t, stdErr, "The variable already exists")

_, _, err = f.RunCombinedOutput("var:create", "-p", projectID, "env:TEST2", "-l", "p", "--value", "1")
assert.NoError(t, err)
assertTrimmed(t, "1", f.Run("var:get", "-p", projectID, "env:TEST2", "-l", "p", "-P", "value"))

_, _, err = f.RunCombinedOutput("var:update", "-p", projectID, "env:TEST2", "-l", "p", "--value", "2")
assert.NoError(t, err)
assertTrimmed(t, "2", f.Run("var:get", "-p", projectID, "env:TEST2", "-l", "p", "-P", "value"))

assertTrimmed(t, "true", f.Run("var:get", "-p", projectID, "env:TEST2", "-l", "p", "-P", "visible_runtime"))
_, _, err = f.RunCombinedOutput("var:update", "-p", projectID, "env:TEST2", "-l", "p", "--visible-runtime", "false")
assert.NoError(t, err)
assertTrimmed(t, "false", f.Run("var:get", "-p", projectID, "env:TEST2", "-l", "p", "-P", "visible_runtime"))
}
3 changes: 1 addition & 2 deletions src/Command/Variable/VariableCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
unset($values['prefix']);
}

$environment = $selection->getEnvironment();

$environment = null;
if (isset($values['environment'])) {
$environment = $this->api->getEnvironment($values['environment'], $selection->getProject());
if (!$environment) {
Expand Down

0 comments on commit 06f5770

Please sign in to comment.