vuln-list-update/utils/fs_test.go

61 lines
1.4 KiB
Go
Raw Normal View History

package utils_test
import (
"errors"
"math"
"testing"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aquasecurity/vuln-list-update/utils"
)
func TestWriteJSON(t *testing.T) {
testCases := []struct {
name string
2019-11-06 18:09:59 +03:00
fs afero.Fs
inputData interface{}
2019-11-06 18:09:59 +03:00
expectedData string
expectedError error
}{
{
2019-11-06 18:09:59 +03:00
name: "happy path",
fs: afero.NewMemMapFs(),
inputData: struct {
A string
B int
}{A: "foo", B: 1},
expectedData: "{\n \"A\": \"foo\",\n \"B\": 1\n}",
},
{
2019-11-06 18:09:59 +03:00
name: "sad path: fs.AppFs.Create returns an error",
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
expectedError: errors.New("unable to create a directory: operation not permitted"),
},
{
name: "sad path: bad json input data",
2019-11-06 18:09:59 +03:00
fs: afero.NewMemMapFs(),
inputData: math.NaN(),
expectedError: errors.New("failed to marshal JSON: json: unsupported value: NaN"),
},
}
for _, tc := range testCases {
err := utils.WriteJSON(tc.fs, "dir", "file", tc.inputData)
switch {
case tc.expectedError != nil:
require.NotNil(t, err)
assert.Equal(t, tc.expectedError.Error(), err.Error(), tc.name)
2019-11-06 18:09:59 +03:00
return
default:
assert.NoError(t, err, tc.name)
}
actual, err := afero.ReadFile(tc.fs, "dir/file")
2019-11-06 18:09:59 +03:00
assert.NoError(t, err, tc.name)
assert.Equal(t, tc.expectedData, string(actual), tc.name)
}
}