Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,23 @@ func runChecks(cfg *config.Config, cfgErr error, deps checkDeps) []result {
return results
}

// Check 5: env files exist (warn only)
// Check 5: env files exist (warn only) + inert files with no checkout vars (info)
for _, ef := range cfg.EnvFiles {
if deps.existingEnvFiles[ef.Path] {
results = append(results, ok(fmt.Sprintf("env file: %s", ef.Path)))
} else {
results = append(results, warn(fmt.Sprintf("env file: %s — not found (will be created on first patch)", ef.Path)))
}

applicable := 0
for _, v := range ef.Vars {
if v.On == "checkout" {
applicable++
}
}
if applicable == 0 {
results = append(results, info(fmt.Sprintf("env file: %s — no vars apply on checkout; file will be left untouched", ef.Path)))
}
}

// Check 6: strategies valid
Expand Down
17 changes: 17 additions & 0 deletions cmd/doctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ func TestRunChecks_EnvFileMissing(t *testing.T) {
}
}

func TestRunChecks_EnvFileNoCheckoutVars(t *testing.T) {
cfg := &config.Config{
Project: "myapp",
EnvFiles: []config.EnvFile{
{Path: ".env", Vars: nil},
},
}
results := runChecks(cfg, nil, happyDeps)
r, found := findByPrefix(results, "env file: .env — no vars apply on checkout")
if !found {
t.Fatalf("expected info line for env file with no checkout vars, got: %v", results)
}
if r.status != "info" {
t.Errorf("expected info status, got %q", r.status)
}
}

func TestRunChecks_UnknownStrategy(t *testing.T) {
cfg := &config.Config{
Project: "myapp",
Expand Down
4 changes: 4 additions & 0 deletions cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func patchEnvFiles(cfg *config.Config, branch string) error {
sensitiveVars[v.Name] = v.Sensitive
}

if len(patches) == 0 {
continue
}

if ef.Backup {
if err := env.BackupFile(ef.Path); err != nil {
return fmt.Errorf("backup %s: %w", ef.Path, err)
Expand Down
106 changes: 106 additions & 0 deletions cmd/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,112 @@ func TestPatchEnvFiles_BackupCreated(t *testing.T) {
}
}

func TestPatchEnvFiles_SkipsWhenNoVars(t *testing.T) {
dir := t.TempDir()
envPath := filepath.Join(dir, ".env")
original := []byte("DB_NAME=keep\n# a comment\nOTHER=val\n")
if err := os.WriteFile(envPath, original, 0600); err != nil {
t.Fatal(err)
}
before, err := os.Stat(envPath)
if err != nil {
t.Fatal(err)
}

cfg := &config.Config{
Project: "myapp",
EnvFiles: []config.EnvFile{
{Path: envPath, Vars: nil},
},
}

if err := patchEnvFiles(cfg, "feature-x"); err != nil {
t.Fatalf("patchEnvFiles: %v", err)
}

got, err := os.ReadFile(envPath)
if err != nil {
t.Fatal(err)
}
if string(got) != string(original) {
t.Errorf("file content changed: got %q, want %q", got, original)
}
after, err := os.Stat(envPath)
if err != nil {
t.Fatal(err)
}
if !before.ModTime().Equal(after.ModTime()) {
t.Errorf("mtime changed: before %v, after %v", before.ModTime(), after.ModTime())
}
}

func TestPatchEnvFiles_SkipsWhenAllVarsNonCheckout(t *testing.T) {
dir := t.TempDir()
envPath := filepath.Join(dir, ".env")
original := []byte("DB_NAME=keep\n")
if err := os.WriteFile(envPath, original, 0600); err != nil {
t.Fatal(err)
}
before, err := os.Stat(envPath)
if err != nil {
t.Fatal(err)
}

cfg := &config.Config{
Project: "myapp",
EnvFiles: []config.EnvFile{
{
Path: envPath,
Vars: []config.Var{
{Name: "DB_NAME", Strategy: "template", On: "db_create"},
},
},
},
}

if err := patchEnvFiles(cfg, "feature-x"); err != nil {
t.Fatalf("patchEnvFiles: %v", err)
}

got, err := os.ReadFile(envPath)
if err != nil {
t.Fatal(err)
}
if string(got) != string(original) {
t.Errorf("file content changed: got %q, want %q", got, original)
}
after, err := os.Stat(envPath)
if err != nil {
t.Fatal(err)
}
if !before.ModTime().Equal(after.ModTime()) {
t.Errorf("mtime changed: before %v, after %v", before.ModTime(), after.ModTime())
}
}

func TestPatchEnvFiles_NoBackupWhenSkipped(t *testing.T) {
dir := t.TempDir()
envPath := filepath.Join(dir, ".env")
if err := os.WriteFile(envPath, []byte("DB_NAME=keep\n"), 0600); err != nil {
t.Fatal(err)
}

cfg := &config.Config{
Project: "myapp",
EnvFiles: []config.EnvFile{
{Path: envPath, Backup: true, Vars: nil},
},
}

if err := patchEnvFiles(cfg, "feature-x"); err != nil {
t.Fatalf("patchEnvFiles: %v", err)
}

if _, err := os.Stat(envPath + ".bak"); !os.IsNotExist(err) {
t.Errorf("expected no backup file, but it exists (or stat err: %v)", err)
}
}

func TestPatchEnvFiles_NoBackupByDefault(t *testing.T) {
dir := t.TempDir()
envPath := filepath.Join(dir, ".env")
Expand Down