mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-27 23:03:37 -05:00
Fix failing test (windows) - simulate an error by executing stat on a filename with zero-byte in it. Fix cleanup of created files after the tests.
This commit is contained in:
parent
6717edcb87
commit
f122b3bbdf
1 changed files with 18 additions and 4 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRoot(t *testing.T) {
|
func TestRoot(t *testing.T) {
|
||||||
|
@ -26,9 +27,12 @@ func TestRoot(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
|
t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
|
||||||
}
|
}
|
||||||
defer os.Remove(existingFile.Name())
|
defer func () {
|
||||||
|
existingFile.Close()
|
||||||
|
os.Remove(existingFile.Name())
|
||||||
|
}()
|
||||||
|
|
||||||
unaccessiblePath := filepath.Join(existingFile.Name(), "some_name")
|
unaccessiblePath := getInaccessibleOsDependentPath(existingFile.Name())
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
input string
|
input string
|
||||||
|
@ -60,8 +64,9 @@ func TestRoot(t *testing.T) {
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
c := NewTestController(test.input)
|
c := NewTestController(test.input)
|
||||||
mid, err := Root(c)
|
mid, err := Root(c)
|
||||||
|
|
||||||
if test.shouldErr && err == nil {
|
if test.shouldErr && err == nil {
|
||||||
t.Errorf("Test %d: Expected error but found nil for input %s", i, test.input)
|
t.Errorf("Test %d: Expected error but found %s for input %s", i, err, test.input)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -97,3 +102,12 @@ func getTempDirPath() (string, error) {
|
||||||
|
|
||||||
return tempDir, nil
|
return tempDir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getInaccessibleOsDependentPath(file string) string{
|
||||||
|
if runtime.GOOS == "windows"{
|
||||||
|
return filepath.Join("C:", "file\x00name")// 0 byte breaks the lstat syscall
|
||||||
|
} else {
|
||||||
|
// TODO - check if the zero-byte filename works for linux only. If it does - use it instead
|
||||||
|
return filepath.Join(file, "some_name")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue