-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper package for filesystem operations
Add package `filesystem` for file system related helper functions. Add `MostRecentFileTimestamp` function to find the most recent file modification timestamp of all files in a given directory.
- Loading branch information
1 parent
95fc6a3
commit 8332e43
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package filesystem_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestFilesystem(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Filesystem Suite") | ||
} | ||
|
||
func withTempDir(f func(target string)) { | ||
GinkgoHelper() | ||
|
||
path, err := os.MkdirTemp(os.TempDir(), "filesystem") | ||
Expect(err).ToNot(HaveOccurred()) | ||
defer os.RemoveAll(path) | ||
|
||
f(path) | ||
} | ||
|
||
func writeTmpFile(parent string, atime time.Time, mtime time.Time) { | ||
tmpFile, err := os.CreateTemp(parent, "test") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(os.WriteFile(tmpFile.Name(), []byte("foobar"), os.FileMode(0644))).To(Succeed()) | ||
Expect(os.Chtimes(tmpFile.Name(), atime, mtime)).To(Succeed()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package filesystem | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
func MostRecentFileTimestamp(root string) (result *time.Time, err error) { | ||
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if d.IsDir() { | ||
return nil | ||
} | ||
|
||
fi, err := os.Lstat(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var modTime = fi.ModTime() | ||
switch { | ||
case result == nil: | ||
result = &modTime | ||
|
||
case result.Before(modTime): | ||
result = &modTime | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return result, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package filesystem_test | ||
|
||
import ( | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/shipwright-io/build/pkg/filesystem" | ||
) | ||
|
||
var _ = Describe("Timestamps", func() { | ||
Context("Figure out timestamps recursively in a directory", func() { | ||
It("should find the most recent timestamp in a given directory", func() { | ||
var ref = time.Now() | ||
|
||
withTempDir(func(target string) { | ||
By("setting up a simple test sample", func() { | ||
for i := 0; i < 10; i++ { | ||
tmpTime := ref.AddDate(0, 0, -i) | ||
writeTmpFile(target, tmpTime, tmpTime) | ||
} | ||
}) | ||
|
||
By("checking if finds the right timestamp", func() { | ||
timestamp, err := MostRecentFileTimestamp(target) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(timestamp).ToNot(BeNil()) | ||
Expect(*timestamp).To(BeTemporally("==", ref)) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |