|
@ -0,0 +1,48 @@ |
|
|
|
|
|
package settings |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
|
"os" |
|
|
|
|
|
"path/filepath" |
|
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/spf13/afero" |
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func TestSettingsParsing(t *testing.T) { |
|
|
|
|
|
const file string = `{ |
|
|
|
|
|
"endpoint": "0.0.0.0:8000", |
|
|
|
|
|
"token": "foobar", |
|
|
|
|
|
"storage_provider": { |
|
|
|
|
|
"type": 0, |
|
|
|
|
|
"base_path": "assets" |
|
|
|
|
|
} |
|
|
|
|
|
}` |
|
|
|
|
|
|
|
|
|
|
|
t.Run("Settings parsing is successful", func(t *testing.T) { |
|
|
|
|
|
settings := parseSettings([]byte(file)) |
|
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) |
|
|
|
|
|
assert.Equal(t, "foobar", settings.Token) |
|
|
|
|
|
assert.Equal(t, "assets", settings.StorageProvider.BasePath) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestSettingsLoading(t *testing.T) { |
|
|
|
|
|
t.Run("Settings loading creates default settings.json when none is present", func(t *testing.T) { |
|
|
|
|
|
fileSystem := afero.NewMemMapFs() |
|
|
|
|
|
workingDirectory, _ := os.Getwd() |
|
|
|
|
|
|
|
|
|
|
|
path := filepath.Join(workingDirectory, "settings.json") |
|
|
|
|
|
|
|
|
|
|
|
// Settings file does not exist in the beginning
|
|
|
|
|
|
doesFileExist, _ := afero.Exists(fileSystem, path) |
|
|
|
|
|
assert.False(t, doesFileExist) |
|
|
|
|
|
|
|
|
|
|
|
LoadSettings(fileSystem) |
|
|
|
|
|
|
|
|
|
|
|
// Settings file should be present after calling LoadSettings
|
|
|
|
|
|
doesFileExist, _ = afero.Exists(fileSystem, path) |
|
|
|
|
|
assert.True(t, doesFileExist) |
|
|
|
|
|
}) |
|
|
|
|
|
} |