2017-01-27 23:39:07 +03:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package dep
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-02-10 22:55:47 +03:00
|
|
|
|
2017-02-10 22:45:09 +03:00
|
|
|
"github.com/golang/dep/test"
|
2017-01-27 23:39:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCopyDir(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "dep")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
srcdir := filepath.Join(dir, "src")
|
2017-02-10 21:08:47 +03:00
|
|
|
if err = os.MkdirAll(srcdir, 0755); err != nil {
|
2017-01-27 23:39:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
srcf, err := os.Create(filepath.Join(srcdir, "myfile"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
want := "hello world"
|
|
|
|
if _, err = srcf.Write([]byte(want)); err != nil {
|
2017-01-27 23:39:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
srcf.Close()
|
|
|
|
|
|
|
|
destdir := filepath.Join(dir, "dest")
|
2017-02-10 21:08:47 +03:00
|
|
|
if err = CopyDir(srcdir, destdir); err != nil {
|
2017-01-27 23:39:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dirOK, err := IsDir(destdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !dirOK {
|
|
|
|
t.Fatalf("expected %s to be a directory", destdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
destf := filepath.Join(destdir, "myfile")
|
2017-02-10 21:08:47 +03:00
|
|
|
got, err := ioutil.ReadFile(destf)
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if want != string(got) {
|
|
|
|
t.Fatalf("expected: %s, got: %s", want, string(got))
|
2017-01-27 23:39:07 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
wantinfo, err := os.Stat(srcf.Name())
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
gotinfo, err := os.Stat(destf)
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if wantinfo.Mode() != gotinfo.Mode() {
|
|
|
|
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode())
|
2017-01-27 23:39:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopyFile(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "dep")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
srcf, err := os.Create(filepath.Join(dir, "srcfile"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
want := "hello world"
|
|
|
|
if _, err := srcf.Write([]byte(want)); err != nil {
|
2017-01-27 23:39:07 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
srcf.Close()
|
|
|
|
|
|
|
|
destf := filepath.Join(dir, "destf")
|
|
|
|
if err := CopyFile(srcf.Name(), destf); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
got, err := ioutil.ReadFile(destf)
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if want != string(got) {
|
|
|
|
t.Fatalf("expected: %s, got: %s", want, string(got))
|
2017-01-27 23:39:07 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
wantinfo, err := os.Stat(srcf.Name())
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
gotinfo, err := os.Stat(destf)
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if wantinfo.Mode() != gotinfo.Mode() {
|
|
|
|
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), wantinfo.Mode(), destf, gotinfo.Mode())
|
2017-01-27 23:39:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsRegular(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]bool{
|
|
|
|
wd: false,
|
2017-02-09 05:37:31 +03:00
|
|
|
filepath.Join(wd, "testdata"): false,
|
2017-01-27 23:39:07 +03:00
|
|
|
filepath.Join(wd, "cmd", "dep", "main.go"): true,
|
|
|
|
filepath.Join(wd, "this_file_does_not_exist.thing"): false,
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
for f, want := range tests {
|
|
|
|
got, err := IsRegular(f)
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
2017-02-10 21:08:47 +03:00
|
|
|
if !want {
|
2017-01-27 23:39:07 +03:00
|
|
|
// this is the case where we expect an error so continue
|
|
|
|
// to the check below
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if got != want {
|
|
|
|
t.Fatalf("expected %t for %s, got %t", want, f, got)
|
2017-01-27 23:39:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsDir(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]bool{
|
|
|
|
wd: true,
|
2017-02-09 05:37:31 +03:00
|
|
|
filepath.Join(wd, "testdata"): true,
|
2017-01-27 23:39:07 +03:00
|
|
|
filepath.Join(wd, "main.go"): false,
|
|
|
|
filepath.Join(wd, "this_file_does_not_exist.thing"): false,
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
for f, want := range tests {
|
|
|
|
got, err := IsDir(f)
|
2017-01-27 23:39:07 +03:00
|
|
|
if err != nil {
|
2017-02-10 21:08:47 +03:00
|
|
|
if !want {
|
2017-01-27 23:39:07 +03:00
|
|
|
// this is the case where we expect an error so continue
|
|
|
|
// to the check below
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Fatalf("expected no error, got %v", err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if got != want {
|
|
|
|
t.Fatalf("expected %t for %s, got %t", want, f, got)
|
2017-01-27 23:39:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-02-10 22:12:13 +03:00
|
|
|
|
|
|
|
func TestIsEmpty(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:45:09 +03:00
|
|
|
h := test.NewHelper(t)
|
|
|
|
h.TempDir("empty")
|
2017-02-10 22:12:13 +03:00
|
|
|
tests := map[string]string{
|
2017-02-10 23:42:02 +03:00
|
|
|
wd: "true",
|
2017-02-11 20:22:25 +03:00
|
|
|
"testdata": "true",
|
2017-02-10 23:42:02 +03:00
|
|
|
filepath.Join(wd, "fs.go"): "err",
|
|
|
|
filepath.Join(wd, "this_file_does_not_exist.thing"): "false",
|
|
|
|
h.Path("empty"): "false",
|
2017-02-10 22:12:13 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 23:24:41 +03:00
|
|
|
for f, want := range tests {
|
2017-02-10 23:42:02 +03:00
|
|
|
empty, err := IsNonEmptyDir(f)
|
2017-02-10 23:24:41 +03:00
|
|
|
if want == "err" {
|
2017-02-10 22:12:13 +03:00
|
|
|
if err == nil {
|
2017-02-10 23:24:41 +03:00
|
|
|
t.Fatalf("Wanted an error for %v, but it was nil", f)
|
2017-02-10 22:12:13 +03:00
|
|
|
}
|
|
|
|
if empty {
|
2017-02-10 23:24:41 +03:00
|
|
|
t.Fatalf("Wanted false with error for %v, but got true", f)
|
2017-02-10 22:12:13 +03:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
2017-02-10 23:24:41 +03:00
|
|
|
t.Fatalf("Wanted no error for %v, got %v", f, err)
|
2017-02-10 22:12:13 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 23:24:41 +03:00
|
|
|
if want == "true" && !empty {
|
|
|
|
t.Fatalf("Wanted true for %v, but got false", f)
|
2017-02-10 22:12:13 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 23:24:41 +03:00
|
|
|
if want == "false" && empty {
|
|
|
|
t.Fatalf("Wanted false for %v, but got true", f)
|
2017-02-10 22:12:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|