Skip to content

Commit

Permalink
Merge pull request getsops#1624 from duthils/test-coverage-errors
Browse files Browse the repository at this point in the history
sops_test: add more tests on errors
  • Loading branch information
felixfontein authored Sep 19, 2024
2 parents e7a1b11 + 8846dc1 commit b11e62a
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions sops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,23 @@ func TestDecrypt(t *testing.T) {
}
}

type WrongType struct{}

func TestEncryptWrongType(t *testing.T) {
branches := TreeBranches{
TreeBranch{
TreeItem{
Key: "foo",
Value: WrongType{},
},
},
}
tree := Tree{Branches: branches, Metadata: Metadata{UnencryptedSuffix: DefaultUnencryptedSuffix}}
result, err := tree.Encrypt(bytes.Repeat([]byte{'f'}, 32), MockCipher{})
assert.Equal(t, "", result)
assert.ErrorContains(t, err, "unknown type: sops.WrongType")
}

func TestTruncateTree(t *testing.T) {
tree := TreeBranch{
TreeItem{
Expand Down Expand Up @@ -875,6 +892,45 @@ func TestTruncateTree(t *testing.T) {
assert.Equal(t, expected, result)
}

func TestTruncateTreeNotFound(t *testing.T) {
tree := TreeBranch{
TreeItem{
Key: "foo",
Value: 2,
},
}
result, err := tree.Truncate([]interface{}{"baz"})
assert.ErrorContains(t, err, "baz")
assert.Nil(t, result, "Truncate result was not nil upon %s", err)
}

func TestTruncateTreeNotArray(t *testing.T) {
tree := TreeBranch{
TreeItem{
Key: "foo",
Value: 2,
},
}
result, err := tree.Truncate([]interface{}{"foo", 99})
assert.ErrorContains(t, err, "99")
assert.Nil(t, result, "Truncate result was not nil upon %s", err)
}

func TestTruncateTreeArrayOutOfBounds(t *testing.T) {
tree := TreeBranch{
TreeItem{
Key: "foo",
Value: []interface{}{
"one",
"two",
},
},
}
result, err := tree.Truncate([]interface{}{"foo", 99})
assert.ErrorContains(t, err, "99")
assert.Nil(t, result, "Truncate result was not nil upon %s", err)
}

func TestEncryptComments(t *testing.T) {
tree := Tree{
Branches: TreeBranches{
Expand Down Expand Up @@ -1360,8 +1416,9 @@ func TestUnsetKeyNotFound(t *testing.T) {
},
},
}
unset, err := branch.Unset([]interface{}{"foo", "unknown"})
assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown")
unset, err := branch.Unset([]interface{}{"foo", "unknown-value"})
assert.Equal(t, err.(*SopsKeyNotFound).Key, "unknown-value")
assert.ErrorContains(t, err, "unknown-value")
assert.Nil(t, unset, "Unset result was not nil upon %s", err)
}

Expand Down Expand Up @@ -1435,6 +1492,10 @@ func TestEmitAsMap(t *testing.T) {
Key: "number",
Value: 42,
},
TreeItem{
Key: Comment{"comment"},
Value: nil,
},
},
TreeBranch{
TreeItem{
Expand Down

0 comments on commit b11e62a

Please sign in to comment.