Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ops: don't re-apply already applied operations #377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -417,10 +416,8 @@ func TestEnv_EphemeralVariableSubstitutionOverride(t *testing.T) {

func opsContains[T any](t *testing.T, slice []T, needle T) {
t.Helper()
for _, el := range slice {
if reflect.DeepEqual(el, needle) {
return
}
if envars.OpsContains(slice, needle) {
return
}
t.Fatalf("%v does not contain %v", slice, needle)
}
17 changes: 16 additions & 1 deletion envars/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ func (e Envars) System() []string {
//
// Envars are not modified.
func (e Envars) Apply(envRoot string, ops Ops) *Transform {
existingOps := Ops{}
if rawExistingOps, err := UnmarshalOps([]byte(e["HERMIT_ENV_OPS"])); err == nil {
existingOps = rawExistingOps
}
transform := transform(envRoot, e)
for _, op := range ops {
op.Apply(transform)
if !OpsContains(existingOps, op) {
op.Apply(transform)
}
}
return transform
}
Expand Down Expand Up @@ -389,6 +395,15 @@ func makeRevertKey(transform *Transform, op Op) string {
return fmt.Sprintf("_HERMIT_OLD_%s_%X", op.Envar(), hash.Sum(nil))
}

func OpsContains[T any](slice []T, needle T) bool {
for _, el := range slice {
if reflect.DeepEqual(el, needle) {
return true
}
}
return false
}

// transform returns a Transform with e as the base.
func transform(envRoot string, e Envars) *Transform {
return &Transform{
Expand Down
12 changes: 12 additions & 0 deletions envars/ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ func TestIssue47(t *testing.T) {
assert.Equal(t, original, reverted)
}

func TestSkipEnvarReapply(t *testing.T) {
original := Envars{
"PATH": "/bin:/usr/bin",
"HERMIT_ENV_OPS": "[{\"p\":{\"n\":\"PATH\",\"v\":\"/usr/bin\"}}]",
}
ops := Ops{
&Prepend{Name: "PATH", Value: "/usr/bin"},
}
actual := original.Apply("/home/user/project", ops).Combined()
assert.Equal(t, original, actual)
}

func TestEncodeDecodeOps(t *testing.T) {
actual := Ops{
&Append{"APPEND", "${APPEND}:text"},
Expand Down