fileutil_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. var fh *os.File
  47. fh, err = os.Create(filepath.Join(tmpdir, f))
  48. if err != nil {
  49. t.Fatalf("error creating file: %v", err)
  50. }
  51. if err := fh.Close(); err != nil {
  52. t.Fatalf("error closing file: %v", err)
  53. }
  54. }
  55. fs, err := ReadDir(tmpdir)
  56. if err != nil {
  57. t.Fatalf("error calling ReadDir: %v", err)
  58. }
  59. wfs := []string{"abc", "def", "ghi", "xyz"}
  60. if !reflect.DeepEqual(fs, wfs) {
  61. t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
  62. }
  63. }