Files
trufflehog/pkg/sources/git/unit_test.go
Miccah c60443891b Add Display method to SourceUnit and Kind member to the CommonSourceUnit (#2450)
* Add Display method to SourceUnit and Kind member to the CommonSourceUnit

* Make SourceUnitID return the ID and a kind

These two values together uniquely represent a unit.
2024-02-20 11:24:13 -08:00

43 lines
1.3 KiB
Go

package git
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnmarshalUnit(t *testing.T) {
s := `{"kind":"repo","id":"https://github.com/trufflesecurity/test_keys.git"}`
expectedUnit := SourceUnit{ID: "https://github.com/trufflesecurity/test_keys.git", Kind: UnitRepo}
gotUnit, err := UnmarshalUnit([]byte(s))
assert.NoError(t, err)
assert.Equal(t, expectedUnit, gotUnit)
_, err = UnmarshalUnit(nil)
assert.Error(t, err)
_, err = UnmarshalUnit([]byte(`{"kind":"idk","id":"id"}`))
assert.Error(t, err)
}
func TestMarshalUnit(t *testing.T) {
unit := SourceUnit{ID: "https://github.com/trufflesecurity/test_keys.git", Kind: UnitRepo}
b, err := json.Marshal(unit)
assert.NoError(t, err)
assert.Equal(t, `{"kind":"repo","id":"https://github.com/trufflesecurity/test_keys.git"}`, string(b))
}
func TestDisplayUnit(t *testing.T) {
unit := SourceUnit{ID: "https://github.com/trufflesecurity/test_keys.git", Kind: UnitRepo}
assert.Equal(t, "trufflesecurity/test_keys", unit.Display())
unit = SourceUnit{ID: "/path/to/repo", Kind: UnitDir}
assert.Equal(t, "repo", unit.Display())
unit = SourceUnit{ID: "ssh://github.com/trufflesecurity/test_keys", Kind: UnitRepo}
assert.Equal(t, "trufflesecurity/test_keys", unit.Display())
}