add tests (thanks to @ts2909)

* copied from 6c031db617
This commit is contained in:
Albert Z Wang 2017-11-29 13:44:12 -05:00
parent 9034a12a59
commit f12a645649
1 changed files with 51 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"path/filepath"
"runtime"
"testing"
"fmt"
)
func TestBasePath(t *testing.T) {
@ -140,3 +141,53 @@ func TestNestedBasePaths(t *testing.T) {
}
}
}
func TestBasePathOpenFile(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
bp := NewBasePathFs(baseFs, "/base/path")
f, err := bp.OpenFile("/tmp/file.txt", os.O_CREATE, 0600)
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
if filepath.Dir(f.Name()) != "/tmp" {
t.Fatalf("realpath leaked: %s", f.Name())
}
}
func TestBasePathCreate(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
bp := NewBasePathFs(baseFs, "/base/path")
f, err := bp.Create("/tmp/file.txt")
if err != nil {
t.Fatalf("failed to create file: %v", err)
}
if filepath.Dir(f.Name()) != "/tmp" {
t.Fatalf("realpath leaked: %s", f.Name())
}
}
func TestBasePathTempFile(t *testing.T) {
baseFs := &MemMapFs{}
baseFs.MkdirAll("/base/path/tmp", 0777)
bp := NewBasePathFs(baseFs, "/base/path")
tDir, err := TempDir(bp, "/tmp", "")
if err != nil {
t.Fatalf("Failed to TempDir: %v", err)
}
fmt.Println(tDir)
if filepath.Dir(tDir) != "/tmp" {
t.Fatalf("Tempdir realpath leaked: %s", tDir)
}
tempFile, err := TempFile(bp, tDir, "")
if err != nil {
t.Fatalf("Failed to TempFile: %v", err)
}
defer tempFile.Close()
if expected, actual := tDir, filepath.Dir(tempFile.Name()); expected != actual {
t.Fatalf("TempFile realpath leaked: expected %s, got %s", expected, actual)
}
}