Skip to content
Snippets Groups Projects
Commit cb80e2bf authored by Jonah Husson's avatar Jonah Husson
Browse files

Merge branch 'release' into xx-4437/no-registration

parents f22171f9 81c2cb07
No related branches found
No related tags found
2 merge requests!40No registration,!34Release
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"time"
) )
const ( const (
...@@ -153,6 +154,17 @@ func DirExists(path string) bool { ...@@ -153,6 +154,17 @@ func DirExists(path string) bool {
return exists && info.IsDir() return exists && info.IsDir()
} }
// GetLastModified returns the time the file was last modified.
func GetLastModified(path string) (time.Time, error) {
// Get file description information and path errors
info, err := os.Stat(path)
if err != nil {
return time.Time{}, err
}
return info.ModTime(), nil
}
// exists checks if a file or directory exists at the specified path and also // exists checks if a file or directory exists at the specified path and also
// returns the file's FileInfo. // returns the file's FileInfo.
func exists(path string) (os.FileInfo, bool) { func exists(path string) (os.FileInfo, bool) {
......
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/require"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
...@@ -517,6 +518,84 @@ func TestDirExists_NoDirError(t *testing.T) { ...@@ -517,6 +518,84 @@ func TestDirExists_NoDirError(t *testing.T) {
} }
} }
// Tests that GetLastModified will return an accurate last modified timestamp.
func TestGetLastModified(t *testing.T) {
path := "test.txt"
data := []byte("Test string.")
// Delete the test file at the end
defer func() {
require.NoError(t, os.RemoveAll(path))
}()
// Record approximately when we are writing to file
firstWriteTimestamp := time.Now()
// Write to file
require.NoError(t, WriteFile(path, data, FilePerms, FilePerms))
// Retrieve the last modification of the file
lastModified, err := GetLastModified(path)
require.NoError(t, err)
// The last modified timestamp should not differ by more than a few
// milliseconds from the timestamp taken before the write operation took
// place.
require.True(t, lastModified.Sub(firstWriteTimestamp) < 2*time.Millisecond ||
lastModified.Sub(firstWriteTimestamp) > 2*time.Millisecond)
// Retrieve modified timestamp again
newLastModified, err := GetLastModified(path)
require.NoError(t, err)
// Ensure last modified does not change arbitrarily
require.Equal(t, newLastModified, lastModified)
}
// Tests that GetLastModified will update after a write operation to a file.
func TestGetLastModified_Update(t *testing.T) {
path := "test.txt"
data := []byte("Test string.")
// Delete the test file at the end
defer func() {
require.NoError(t, os.RemoveAll(path))
}()
// Write to file
require.NoError(t, WriteFile(path, data, FilePerms, FilePerms))
// Retrieve the last modification of the file
lastModified, err := GetLastModified(path)
require.NoError(t, err)
time.Sleep(50 * time.Millisecond)
// Record timestamp of second write
secondWriteTimestamp := time.Now()
// Write again to the same file path
newData := []byte("New data")
require.NoError(t, WriteFile(path, newData, FilePerms, FilePerms))
// Retrieve last modified after re-writing to file
newLastModified, err := GetLastModified(path)
require.NoError(t, err)
// Ensure last modified has been updated, and is not returning an old value
require.NotEqual(t, newLastModified, lastModified)
// The last modified timestamp should not differ by more than a few
// milliseconds from the timestamp taken before the write operation took
// place.
require.True(t, lastModified.Sub(secondWriteTimestamp) < 2*time.Millisecond ||
lastModified.Sub(secondWriteTimestamp) > 2*time.Millisecond)
}
// Tests that Test_exist correctly finds a file that exists and returns the // Tests that Test_exist correctly finds a file that exists and returns the
// correct FileInfo. // correct FileInfo.
func Test_exist(t *testing.T) { func Test_exist(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment