-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file utility functions for atomic writing and existence checking;…
… enhance report retrieval logic
- Loading branch information
Showing
13 changed files
with
247 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
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,48 @@ | ||
// Package fileutils provides utility functions for handling files. | ||
package fileutils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// AtomicWrite writes data to a file atomically. | ||
// If the file already exists, then it will be overwritten. | ||
// Not atomic on Windows. | ||
func AtomicWrite(path string, data []byte) error { | ||
tmp, err := os.CreateTemp(filepath.Dir(path), "consent-*.tmp") | ||
if err != nil { | ||
return fmt.Errorf("could not create temporary file: %v", err) | ||
} | ||
defer func() { | ||
_ = tmp.Close() | ||
if err := os.Remove(tmp.Name()); err != nil && !os.IsNotExist(err) { | ||
slog.Warn("Failed to remove temporary file", "file", tmp.Name(), "error", err) | ||
} | ||
}() | ||
|
||
if _, err := tmp.Write(data); err != nil { | ||
return fmt.Errorf("could not write to temporary file: %v", err) | ||
} | ||
|
||
if err := tmp.Close(); err != nil { | ||
return fmt.Errorf("could not close temporary file: %v", err) | ||
} | ||
|
||
if err := os.Rename(tmp.Name(), path); err != nil { | ||
return fmt.Errorf("could not rename temporary file: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
// FileExists checks if a file exists at the given path. | ||
func FileExists(path string) (bool, error) { | ||
_, err := os.Stat(path) | ||
if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return false, err | ||
} | ||
return !errors.Is(err, os.ErrNotExist), nil | ||
} |
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,103 @@ | ||
package fileutils_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/ubuntu/ubuntu-insights/internal/fileutils" | ||
) | ||
|
||
func TestAtomicWrite(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
data []byte | ||
fileExists bool | ||
|
||
wantError bool | ||
}{ | ||
"Empty file": {data: []byte{}, fileExists: false}, | ||
"Non-empty file": {data: []byte("data"), fileExists: false}, | ||
"Override file": {data: []byte("data"), fileExists: true}, | ||
"Override empty file": {data: []byte{}, fileExists: true}, | ||
|
||
"Existing empty file": {data: []byte{}, fileExists: true}, | ||
"Existing non-empty file": {data: []byte("data"), fileExists: true}, | ||
} | ||
|
||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
oldFile := []byte("Old File!") | ||
tempDir := t.TempDir() | ||
path := filepath.Join(tempDir, "file") | ||
if tc.fileExists { | ||
err := fileutils.AtomicWrite(path, oldFile) | ||
require.NoError(t, err, "Setup: AtomicWrite should not return an error") | ||
} | ||
|
||
err := fileutils.AtomicWrite(path, tc.data) | ||
if tc.wantError { | ||
require.Error(t, err, "AtomicWrite should return an error") | ||
|
||
// Check that the file was not overwritten | ||
data, err := os.ReadFile(path) | ||
require.NoError(t, err, "ReadFile should not return an error") | ||
require.Equal(t, oldFile, data, "AtomicWrite should not overwrite the file") | ||
} else { | ||
require.NoError(t, err, "AtomicWrite should not return an error") | ||
|
||
// Check that the file was written | ||
data, err := os.ReadFile(path) | ||
require.NoError(t, err, "ReadFile should not return an error") | ||
require.Equal(t, tc.data, data, "AtomicWrite should write the data to the file") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFileExists(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := map[string]struct { | ||
fileExists bool | ||
parentDirIsFile bool | ||
|
||
wantExists bool | ||
wantError bool | ||
}{ | ||
"Returns_true_when_file_exists": {fileExists: true, wantExists: true}, | ||
"Returns_false_when_file_does_not_exist": {fileExists: false, wantExists: false}, | ||
"Returns_false_when_parent_directory_does_not_exist": {fileExists: false, wantExists: false}, | ||
|
||
"Error_when_parent_directory_is_a_file": {parentDirIsFile: true, wantError: true}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempDir := t.TempDir() | ||
path := filepath.Join(tempDir, "file") | ||
if tc.fileExists { | ||
err := fileutils.AtomicWrite(path, []byte{}) | ||
require.NoError(t, err, "AtomicWrite should not return an error") | ||
} | ||
if tc.parentDirIsFile { | ||
path = filepath.Join(tempDir, "file", "file") | ||
err := fileutils.AtomicWrite(filepath.Join(tempDir, "file"), []byte{}) | ||
require.NoError(t, err, "AtomicWrite should not return an error") | ||
} | ||
|
||
exists, err := fileutils.FileExists(path) | ||
if tc.wantError { | ||
require.Error(t, err, "FileExists should return an error") | ||
} else { | ||
require.NoError(t, err, "FileExists should not return an error") | ||
} | ||
require.Equal(t, tc.wantExists, exists, "FileExists should return the expected result") | ||
}) | ||
} | ||
} |
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
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
1 change: 1 addition & 0 deletions
1
internal/reportutils/testdata/TestGetReports/golden/empty_directory
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 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
internal/reportutils/testdata/TestGetReports/golden/files_in_subdir
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 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
internal/reportutils/testdata/TestGetReports/golden/get_newest_of_period
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 @@ | ||
0: 7 |
1 change: 1 addition & 0 deletions
1
internal/reportutils/testdata/TestGetReports/golden/invalid_file_extension
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 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
internal/reportutils/testdata/TestGetReports/golden/invalid_file_names
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 @@ | ||
{} |
1 change: 1 addition & 0 deletions
1
internal/reportutils/testdata/TestGetReports/golden/mix_of_valid_and_invalid
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 @@ | ||
0: 2 |
3 changes: 3 additions & 0 deletions
3
internal/reportutils/testdata/TestGetReports/golden/multiple_consequtive_windows
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,3 @@ | ||
0: 7 | ||
100: 107 | ||
200: 207 |
3 changes: 3 additions & 0 deletions
3
internal/reportutils/testdata/TestGetReports/golden/multiple_non-consequtive_windows
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,3 @@ | ||
0: 7 | ||
100: 107 | ||
250: 257 |