Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
primitives
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xx network
primitives
Commits
cb80e2bf
Commit
cb80e2bf
authored
Feb 15, 2023
by
Jonah Husson
Browse files
Options
Downloads
Plain Diff
Merge branch 'release' into xx-4437/no-registration
parents
f22171f9
81c2cb07
No related branches found
No related tags found
2 merge requests
!40
No registration
,
!34
Release
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
utils/utils.go
+21
-9
21 additions, 9 deletions
utils/utils.go
utils/utils_test.go
+79
-0
79 additions, 0 deletions
utils/utils_test.go
with
100 additions
and
9 deletions
utils/utils.go
+
21
−
9
View file @
cb80e2bf
...
@@ -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
)
{
...
...
This diff is collapsed.
Click to expand it.
utils/utils_test.go
+
79
−
0
View file @
cb80e2bf
...
@@ -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
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment