fileutil_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutil
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "testing"
  21. )
  22. func TestIsDirWriteable(t *testing.T) {
  23. tmpdir, err := ioutil.TempDir("", "")
  24. if err != nil {
  25. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  26. }
  27. defer os.RemoveAll(tmpdir)
  28. if err := IsDirWriteable(tmpdir); err != nil {
  29. t.Fatalf("unexpected IsDirWriteable error: %v", err)
  30. }
  31. if err := os.Chmod(tmpdir, 0444); err != nil {
  32. t.Fatalf("unexpected os.Chmod error: %v", err)
  33. }
  34. if err := IsDirWriteable(tmpdir); err == nil {
  35. t.Fatalf("expected IsDirWriteable to error")
  36. }
  37. }
  38. func TestReadDir(t *testing.T) {
  39. tmpdir, err := ioutil.TempDir("", "")
  40. defer os.RemoveAll(tmpdir)
  41. if err != nil {
  42. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  43. }
  44. files := []string{"def", "abc", "xyz", "ghi"}
  45. for _, f := range files {
  46. fh, err := os.Create(filepath.Join(tmpdir, f))
  47. if err != nil {
  48. t.Fatalf("error creating file: %v", err)
  49. }
  50. if err := fh.Close(); err != nil {
  51. t.Fatalf("error closing file: %v", err)
  52. }
  53. }
  54. fs, err := ReadDir(tmpdir)
  55. if err != nil {
  56. t.Fatalf("error calling ReadDir: %v", err)
  57. }
  58. wfs := []string{"abc", "def", "ghi", "xyz"}
  59. if !reflect.DeepEqual(fs, wfs) {
  60. t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
  61. }
  62. }