11
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadProfiles_missingFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
data, err := ReadProfiles(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
wantProfiles := []string{"dev", "prod"}
|
||||
if !reflect.DeepEqual(data.Profiles, wantProfiles) {
|
||||
t.Errorf("Profiles = %v, want %v", data.Profiles, wantProfiles)
|
||||
}
|
||||
if len(data.Rows) != 0 {
|
||||
t.Errorf("Rows should be empty, got %v", data.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProfiles_roundtrip(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
original := ProfilesData{
|
||||
Profiles: []string{"staging", "prod", "dev"},
|
||||
Rows: []ProfileRow{
|
||||
{Key: "DB_URL", Values: map[string]string{"dev": "localhost", "prod": "prod.db"}},
|
||||
{Key: "API_KEY", Values: map[string]string{"dev": "dev-key", "staging": "stage-key"}},
|
||||
},
|
||||
}
|
||||
if err := WriteProfiles(dir, original); err != nil {
|
||||
t.Fatalf("WriteProfiles: %v", err)
|
||||
}
|
||||
got, err := ReadProfiles(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadProfiles: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, original) {
|
||||
t.Errorf("roundtrip mismatch:\n got %+v\n want %+v", got, original)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProfiles_jsonTags(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
spxDir := filepath.Join(dir, ".spx")
|
||||
if err := os.MkdirAll(spxDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw := `{"profiles":["x"],"rows":[{"key":"K","values":{"x":"v"}}]}` + "\n"
|
||||
if err := os.WriteFile(filepath.Join(spxDir, "profiles.json"), []byte(raw), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := ReadProfiles(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadProfiles: %v", err)
|
||||
}
|
||||
if len(data.Profiles) != 1 || data.Profiles[0] != "x" {
|
||||
t.Errorf("Profiles = %v, want [x]", data.Profiles)
|
||||
}
|
||||
if len(data.Rows) != 1 || data.Rows[0].Key != "K" || data.Rows[0].Values["x"] != "v" {
|
||||
t.Errorf("Rows = %v, unexpected", data.Rows)
|
||||
}
|
||||
|
||||
// verify written JSON uses lowercase tags
|
||||
if err := WriteProfiles(dir, data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
written, _ := os.ReadFile(filepath.Join(spxDir, "profiles.json"))
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(written, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := m["profiles"]; !ok {
|
||||
t.Error("written JSON missing 'profiles' key")
|
||||
}
|
||||
if _, ok := m["rows"]; !ok {
|
||||
t.Error("written JSON missing 'rows' key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteProfiles_createsSpxDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
// .spx does not exist yet
|
||||
if err := WriteProfiles(dir, ProfilesData{Profiles: []string{"a"}, Rows: nil}); err != nil {
|
||||
t.Fatalf("WriteProfiles: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, ".spx", "profiles.json")); err != nil {
|
||||
t.Errorf("profiles.json not created: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user