github.com/sc0rp1us/gb@v0.4.1-0.20160319180011-4ba8cf1baa5a/fileutils/path_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  package fileutils
     5  
     6  import (
     7  	"os"
     8  	"runtime"
     9  	"testing"
    10  )
    11  
    12  var isReadonlyError = func(error) bool { return false }
    13  
    14  func TestRemoveAll(t *testing.T) {
    15  	tmpDir := os.TempDir()
    16  	// Work directory.
    17  	path := tmpDir + "/_TestRemoveAll_"
    18  	fpath := path + "/file"
    19  	dpath := path + "/dir"
    20  
    21  	// Make directory with 1 file and remove.
    22  	if err := os.MkdirAll(path, 0777); err != nil {
    23  		t.Fatalf("MkdirAll %q: %s", path, err)
    24  	}
    25  	fd, err := os.Create(fpath)
    26  	if err != nil {
    27  		t.Fatalf("create %q: %s", fpath, err)
    28  	}
    29  	fd.Close()
    30  	if err = RemoveAll(path); err != nil {
    31  		t.Fatalf("RemoveAll %q (first): %s", path, err)
    32  	}
    33  	if _, err = os.Lstat(path); err == nil {
    34  		t.Fatalf("Lstat %q succeeded after RemoveAll (first)", path)
    35  	}
    36  
    37  	// Make directory with file and subdirectory and remove.
    38  	if err = os.MkdirAll(dpath, 0777); err != nil {
    39  		t.Fatalf("MkdirAll %q: %s", dpath, err)
    40  	}
    41  	fd, err = os.Create(fpath)
    42  	if err != nil {
    43  		t.Fatalf("create %q: %s", fpath, err)
    44  	}
    45  	fd.Close()
    46  	fd, err = os.Create(dpath + "/file")
    47  	if err != nil {
    48  		t.Fatalf("create %q: %s", fpath, err)
    49  	}
    50  	fd.Close()
    51  	if err = RemoveAll(path); err != nil {
    52  		t.Fatalf("RemoveAll %q (second): %s", path, err)
    53  	}
    54  	if _, err := os.Lstat(path); err == nil {
    55  		t.Fatalf("Lstat %q succeeded after RemoveAll (second)", path)
    56  	}
    57  
    58  	// Determine if we should run the following test.
    59  	testit := true
    60  	if runtime.GOOS == "windows" {
    61  		// Chmod is not supported under windows.
    62  		testit = false
    63  	} else {
    64  		// Test fails as root.
    65  		testit = os.Getuid() != 0
    66  	}
    67  	if testit {
    68  		// Make directory with file and subdirectory and trigger error.
    69  		if err = os.MkdirAll(dpath, 0777); err != nil {
    70  			t.Fatalf("MkdirAll %q: %s", dpath, err)
    71  		}
    72  
    73  		for _, s := range []string{fpath, dpath + "/file1", path + "/zzz"} {
    74  			fd, err = os.Create(s)
    75  			if err != nil {
    76  				t.Fatalf("create %q: %s", s, err)
    77  			}
    78  			fd.Close()
    79  		}
    80  		if err = os.Chmod(dpath, 0); err != nil {
    81  			t.Fatalf("Chmod %q 0: %s", dpath, err)
    82  		}
    83  
    84  		// No error checking here: either RemoveAll
    85  		// will or won't be able to remove dpath;
    86  		// either way we want to see if it removes fpath
    87  		// and path/zzz.  Reasons why RemoveAll might
    88  		// succeed in removing dpath as well include:
    89  		//	* running as root
    90  		//	* running on a file system without permissions (FAT)
    91  		RemoveAll(path)
    92  		os.Chmod(dpath, 0777)
    93  
    94  		for _, s := range []string{fpath, path + "/zzz"} {
    95  			if _, err = os.Lstat(s); err == nil {
    96  				t.Fatalf("Lstat %q succeeded after partial RemoveAll", s)
    97  			}
    98  		}
    99  	}
   100  	if err = RemoveAll(path); err != nil {
   101  		t.Fatalf("RemoveAll %q after partial RemoveAll: %s", path, err)
   102  	}
   103  	if _, err = os.Lstat(path); err == nil {
   104  		t.Fatalf("Lstat %q succeeded after RemoveAll (final)", path)
   105  	}
   106  }