Skip to content

Commit

Permalink
tests: add coverage for unset
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Fontein <[email protected]>
Signed-off-by: Sebastien Duthil <[email protected]>
  • Loading branch information
duthils and felixfontein committed Sep 17, 2024
1 parent a8d6148 commit 836932c
Showing 1 changed file with 308 additions and 0 deletions.
308 changes: 308 additions & 0 deletions sops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,314 @@ func TestSetArrayNonLeaf(t *testing.T) {
}, set)
}

func TestUnsetKeyRootLeaf(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: "foo",
},
TreeItem{
Key: "foofoo",
Value: "foofoo",
},
}
unset, err := branch.Unset([]interface{}{"foofoo"})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: "foo",
},
}, unset)
}

func TestUnsetKeyBranchLeaf(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
TreeItem{
Key: "barbar",
Value: "barbar",
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", "barbar"})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
}, unset)
}

func TestUnsetKeyBranch(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: "foo",
},
TreeItem{
Key: "foofoo",
Value: TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
}
unset, err := branch.Unset([]interface{}{"foofoo"})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: "foo",
},
}, unset)
}

func TestUnsetKeyRootLastLeaf(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: "foo",
},
}
unset, err := branch.Unset([]interface{}{"foo"})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
}, unset)
}

func TestUnsetKeyBranchLastLeaf(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", "bar"})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: TreeBranch{
},
},
}, unset)
}

func TestUnsetKeyArray(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: TreeBranch{
TreeItem{
Key: "bar",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "baz",
Value: "baz",
},
},
},
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", "bar"})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: TreeBranch{
},
},
}, unset)
}

func TestUnsetArrayItem(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
TreeBranch{
TreeItem{
Key: "barbar",
Value: "barbar",
},
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", 1})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
},
}, unset)
}

func TestUnsetKeyInArrayItem(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
TreeItem{
Key: "barbar",
Value: "barbar",
},
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", 0, "barbar"})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
},
}, unset)
}

func TestUnsetArrayLastItem(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", 0})
assert.Nil(t, err)
assert.Equal(t, TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
},
},
}, unset)
}

func TestUnsetKeyNotFound(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", "unknown"})
assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown")
assert.Nil(t, unset, "Unset result was not nil upon %s", err)
}

func TestUnsetKeyInArrayNotFound(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", 0, "unknown"})
assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown")
assert.Nil(t, unset, "Unset result was not nil upon %s", err)
}

func TestUnsetArrayItemOutOfBounds(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
TreeBranch{
TreeItem{
Key: "bar",
Value: "bar",
},
},
},
},
}
unset, err := branch.Unset([]interface{}{"foo", 99})
assert.Equal(t, err.(*SopsKeyNotFound).Key, 99)
assert.Nil(t, unset, "Unset result was not nil upon %s", err)
}

func TestUnsetKeyNotABranch(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo",
Value: 99,
},
}
unset, err := branch.Unset([]interface{}{"foo", "bar"})
assert.Contains(t, err.Error(), "Unsupported type")
assert.Nil(t, unset, "Unset result was not nil upon %s", err)
}

func TestEmitAsMap(t *testing.T) {
expected := map[string]interface{}{
"foobar": "barfoo",
Expand Down

0 comments on commit 836932c

Please sign in to comment.