Skip to content

Commit

Permalink
Add helper package for filesystem operations
Browse files Browse the repository at this point in the history
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
HeavyWombat committed Jan 18, 2024
1 parent 95fc6a3 commit 8332e43
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
36 changes: 36 additions & 0 deletions pkg/filesystem/filesystem_suite_test.go
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())
}
42 changes: 42 additions & 0 deletions pkg/filesystem/timestamps.go
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
}
38 changes: 38 additions & 0 deletions pkg/filesystem/timestamps_test.go
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))
})
})
})
})
})

0 comments on commit 8332e43

Please sign in to comment.