fix(settings): create parent dir in raw settings save path · Entire

fix(settings): create parent dir in raw settings save path

1e866fe→main·

suhaanthayyil·3d ago·2 files·+46 added/-0 removed

The raw save path (saveRaw -> WriteFileAtomic) never created the parent directory, unlike the struct save path (saveToFile) it replaced. A raw enabled-flag flip in a repo that has never created .entire/ therefore hard-failed with "no such file or directory" (e.g. a bare entire disable in a fresh repo). MkdirAll the parent, mirroring saveToFile.

Changes

2

629 unmodified lines

630
631
632
633
634
635
636
637
638
639
640
641
642

629 unmodified lines

if err != nil {
        return fmt.Errorf("marshal %s settings: %w", label, err)
    }
    // Ensure the parent directory exists, mirroring the struct save path
    // (saveToFile). Without this, the raw save path fails in a repo that has
    // never created .entire/ — e.g. a bare `entire disable` in a fresh repo,
    // which resolves to a raw flip before any directory is created.
    if err := os.MkdirAll(filepath.Dir(path), 0o750); err != nil {
        return fmt.Errorf("creating %s settings directory: %w", label, err)
    }
    if err := jsonutil.WriteFileAtomic(path, data, 0o644); err != nil {
        return fmt.Errorf("writing %s settings: %w", label, err)
    }

Mcmd/entire/cli/settings/settings.go+7

1329 unmodified lines

1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371

1329 unmodified lines

t.Error("merge(nil, emptyNonNil) should return a non-nil empty map, got nil")
    }
}

// TestSaveProjectRaw_CreatesMissingParentDir verifies the raw save path creates
// its parent directory, mirroring the struct save path (saveToFile). Without
// this, a raw enabled-flag flip in a repo that has never created .entire/
// (e.g. a bare `entire disable` in a fresh repo) hard-fails with "no such file
// or directory". Regression test for the saveRaw MkdirAll fix.
func TestSaveProjectRaw_CreatesMissingParentDir(t *testing.T) {
    tmpDir := t.TempDir()
    path := filepath.Join(tmpDir, ".entire", "settings.json")

raw := map[string]json.RawMessage{"enabled": json.RawMessage("false")}
    if err := SaveProjectRaw(path, raw); err != nil {
        t.Fatalf("SaveProjectRaw() into a missing .entire dir should succeed, got: %%v", err)
    }

data, err := os.ReadFile(path)
    if err != nil {
        t.Fatalf("settings file should have been created: %%v", err)
    }
    if !strings.Contains(string(data), `"enabled": false`) {
        t.Errorf("expected enabled:false, got: %%s", data)
    }
}

// TestSaveLocalRaw_CreatesMissingParentDir is the local-scope mirror of
// TestSaveProjectRaw_CreatesMissingParentDir.
func TestSaveLocalRaw_CreatesMissingParentDir(t *testing.T) {
    tmpDir := t.TempDir()
    path := filepath.Join(tmpDir, ".entire", "settings.local.json")

raw := map[string]json.RawMessage{"enabled": json.RawMessage("false")}
    if err := SaveLocalRaw(path, raw); err != nil {
        t.Fatalf("SaveLocalRaw() into a missing .entire dir should succeed, got: %%v", err)
    }

if _, err := os.ReadFile(path); err != nil {
        t.Fatalf("local settings file should have been created: %%v", err)
    }
}

Mcmd/entire/cli/settings/settings_test.go+39