From 5a7d7ddc13f4f74996d1c05696189cbba10d7783 Mon Sep 17 00:00:00 2001 From: Andreas Paul Date: Wed, 25 May 2022 17:33:00 +0200 Subject: [PATCH] fix .resource_types folder and content purge skip (#198) * fix .resource_types folder and content purge skip * remove .resource_types from purge_allowlist as it should be allowed by default * add correct test of implicit .resource_types purge prevention --- g10k_test.go | 11 ++++++++++- stale.go | 4 ++-- tests/TestConfigExamplePurgeEnvironment.yaml | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/g10k_test.go b/g10k_test.go index 0a80511..74d5fe2 100755 --- a/g10k_test.go +++ b/g10k_test.go @@ -2217,6 +2217,11 @@ func TestPurgeStaleContent(t *testing.T) { defer f.Close() f.WriteString("foobar") f.Sync() + createOrPurgeDir("/tmp/example/single/.resource_types", funcName) + r, _ := os.Create("/tmp/example/single/.resource_types/test.pp") + defer r.Close() + r.WriteString("foobar") + r.Sync() cmd := exec.Command(os.Args[0], "-test.run="+funcName+"$") cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+funcName+"=1") @@ -2231,7 +2236,7 @@ func TestPurgeStaleContent(t *testing.T) { if expectedExitCode != exitCode { t.Errorf("terminated with %v, but we expected exit status %v", exitCode, expectedExitCode) } - //fmt.Println(string(out)) + // fmt.Println(string(out)) expectedLines := []string{ "DEBUG checkForStaleContent(): filepath.Walk'ing directory /tmp/example/single", @@ -2259,6 +2264,10 @@ func TestPurgeStaleContent(t *testing.T) { t.Errorf("Missing module file that should be there") } + if !fileExists("/tmp/example/single/.resource_types/test.pp") { + t.Errorf("Missing resource_types file /tmp/example/single/.resource_types/test.pp that should be there") + } + purgeDir(cacheDir, funcName) purgeDir("/tmp/example", funcName) } diff --git a/stale.go b/stale.go index df8faf7..38d1bb8 100644 --- a/stale.go +++ b/stale.go @@ -95,9 +95,9 @@ func checkForStaleContent(workDir string) { } checkForStaleContent := func(path string, info os.FileInfo, err error) error { - //Debugf("filepath.Walk'ing found path: " + path) stale := true - if strings.HasSuffix(path, ".resource_types") && isDir(path) { + // never purge .resource_types dir and its content + if strings.HasSuffix(path, ".resource_types") || strings.HasSuffix(filepath.Dir(path), ".resource_types") { stale = false } else { for _, desiredFile := range desiredContent { diff --git a/tests/TestConfigExamplePurgeEnvironment.yaml b/tests/TestConfigExamplePurgeEnvironment.yaml index 6ae024e..bb8c9c9 100644 --- a/tests/TestConfigExamplePurgeEnvironment.yaml +++ b/tests/TestConfigExamplePurgeEnvironment.yaml @@ -1,7 +1,7 @@ --- :cachedir: '/tmp/g10k' purge_levels: ['environment'] -purge_allowlist: [ '.latest_revision', '.resource_types', '.resource_types/*.pp' ] +purge_allowlist: [ '.latest_revision' ] sources: example: