-
Notifications
You must be signed in to change notification settings - Fork 18
test: adapt test cases for Windows 10 environment #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+124
−104
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd16c5a
fix(test): skip Windows permission tests, close unclosed resources, i…
qiyoulin-ops 3217135
Maintain backward compatibility with previous Go versions
qiyoulin-ops 29e9418
Skip inappropriate test cases when running as root
qiyoulin-ops fd4bf5c
Update timberjack_linux_test.go
DeRuina ec011f2
Update timberjack_linux_test.go
DeRuina 3041e5b
Merge branch 'main' into main
DeRuina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,116 @@ | ||
| package timberjack | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRotate_OpenNewFails(t *testing.T) { | ||
| if os.Getuid() == 0 { | ||
| t.Skip("Skipping test when running as root") | ||
| } | ||
| badPath := "/bad/path/logfile.log" | ||
| if _, err := os.Stat(badPath); err == nil { | ||
| t.Skip("Skipping test that relies on non-existent path") | ||
| } | ||
| l := &Logger{ | ||
| Filename: badPath, | ||
| } | ||
| // force an invalid path to trigger openNew failure | ||
| err := l.rotate("manual") | ||
| if err == nil { | ||
| t.Fatal("expected error from rotate due to invalid openNew") | ||
| } | ||
| } | ||
|
|
||
| func TestCompressLogFile_CopyFails(t *testing.T) { | ||
| if os.Getuid() == 0 { | ||
| t.Skip("Skipping test when running as root") | ||
| } | ||
| dir := t.TempDir() | ||
| src := filepath.Join(dir, "bad.log") | ||
| dst := src + ".gz" | ||
|
|
||
| if err := os.WriteFile(src, []byte("data"), 0o200); err != nil { // write-only | ||
| t.Fatalf("failed to create test file: %v", err) | ||
| } | ||
| defer os.Chmod(src, 0o644) | ||
|
|
||
| originalStat := osStat | ||
| osStat = func(name string) (os.FileInfo, error) { | ||
| return os.Stat(src) | ||
| } | ||
| defer func() { osStat = originalStat }() | ||
|
|
||
| l := &Logger{} | ||
| // snapshot patched osStat | ||
| l.resolveConfigLocked() | ||
|
|
||
| err := l.compressLogFile(src, dst) | ||
| if err == nil { | ||
| t.Errorf("expected failure during compression, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestOpenNewDefaultPerm(t *testing.T) { | ||
| // Ensure no bits get masked out. | ||
| syscall.Umask(0o000) | ||
|
|
||
| dir := makeTempDir("TestOpenNewDefaultPerm", t) | ||
| defer os.RemoveAll(dir) | ||
|
|
||
| l := &Logger{ | ||
| Filename: logFile(dir), | ||
| } | ||
| defer l.Close() | ||
|
|
||
| _, err := l.Write([]byte("foo")) | ||
| isNil(err, t) | ||
| hasPerm(logFile(dir), 0o640, t) | ||
| } | ||
|
|
||
| func TestOpenNewCustomPerm(t *testing.T) { | ||
| // Ensure no bits get masked out. | ||
| syscall.Umask(0o000) | ||
|
|
||
|
DeRuina marked this conversation as resolved.
DeRuina marked this conversation as resolved.
|
||
| dir := makeTempDir("TestOpenNewCustomPerm", t) | ||
| defer os.RemoveAll(dir) | ||
|
|
||
| filename := logFile(dir) | ||
| l1 := &Logger{ | ||
| Filename: filename, | ||
| FileMode: 0o747, | ||
| } | ||
| t.Cleanup(func() { | ||
| l1.Close() | ||
| }) | ||
| _, err := l1.Write([]byte("foo")) | ||
| isNil(err, t) | ||
| hasPerm(filename, 0o747, t) | ||
|
|
||
| filename += ".1" | ||
| l2 := &Logger{ | ||
| Filename: filename, | ||
| FileMode: 0o200, | ||
| } | ||
| t.Cleanup(func() { | ||
| l2.Close() | ||
| }) | ||
| _, err = l2.Write([]byte("foo")) | ||
| isNil(err, t) | ||
| hasPerm(filename, 0o200, t) | ||
|
|
||
| filename += ".2" | ||
| l3 := &Logger{ | ||
| Filename: filename, | ||
| FileMode: 0o666, | ||
| } | ||
| t.Cleanup(func() { | ||
| l3.Close() | ||
| }) | ||
| _, err = l3.Write([]byte("foo")) | ||
| isNil(err, t) | ||
| hasPerm(filename, 0o666, t) | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.