func Example() { tmpDir, err := ioutil.TempDir("", "go-arg-test") if err != nil { fmt.Println("TempDir error") return } defer os.RemoveAll(tmpDir) os.Args = make([]string, 3) os.Args[0] = "example" os.Args[1] = filepath.Join(tmpDir, "input") os.Args[2] = filepath.Join(tmpDir, "output") fmt.Println(fval.FileExists(os.Args[1])) os.Create(os.Args[1]) fmt.Println(fval.FileExists(os.Args[1])) exists, err := fval.DirExistsOrCreate(os.Args[2], 0766) fmt.Println(exists) fmt.Println(err) fmt.Println(fval.Exists(os.Args[1])) fmt.Println(fval.Exists(os.Args[2])) // Output: // false // true // true // <nil> // true // true }
func TestDirExistsOrCreate(t *testing.T) { tmpDir := createTmpDir(t) defer os.RemoveAll(tmpDir) path := filepath.Join(tmpDir, "test1", "test2", "test3") ok, err := fval.DirExistsOrCreate(path, 0766) if err != nil { t.Errorf("Error creating dir: %s", err) } if !ok { t.Errorf("Directory was not created") } ok, err = fval.DirExistsOrCreate(path, 0766) if err != nil { t.Errorf("Error: %s", err) } if !ok { t.Errorf("Directory did not exist") } os.Remove(path) if fval.DirExists(path) { t.Errorf("Directory %s should not exist", path) } ok, err = fval.DirExistsOrCreate(path, 0766) if err != nil { t.Errorf("Error creating dir: %s", err) } if !ok { t.Errorf("Directory was not created") } if !fval.DirExists(path) { t.Errorf("Directory %s was not created", path) } }
func TestDirExistsOrCreateError(t *testing.T) { tmpDir := createTmpDir(t) defer os.RemoveAll(tmpDir) path := filepath.Join(tmpDir, "test") os.Mkdir(path, 0400) path = filepath.Join(path, "test2") ok, err := fval.DirExistsOrCreate(path, 0766) if ok { t.Errorf("Directory should not have been created") } if err == nil { t.Errorf("Error should not have been nil") } }