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

unit tests: add coverage for unset #1617

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
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
Loading