2017-01-18 01:07:50 +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.
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
package dep
|
2016-10-18 18:05:34 +03:00
|
|
|
|
|
|
|
import (
|
2017-09-02 11:21:20 +03:00
|
|
|
"fmt"
|
2016-10-18 18:05:34 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-01-14 00:51:15 +03:00
|
|
|
"runtime"
|
2017-09-02 11:21:20 +03:00
|
|
|
"strings"
|
2016-10-18 18:05:34 +03:00
|
|
|
"testing"
|
2017-01-25 04:40:23 +03:00
|
|
|
|
2017-05-10 07:13:22 +03:00
|
|
|
"github.com/golang/dep/internal/gps"
|
2017-05-10 07:24:12 +03:00
|
|
|
"github.com/golang/dep/internal/test"
|
2016-10-18 18:05:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindRoot(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-27 23:39:07 +03:00
|
|
|
|
2017-02-09 05:37:31 +03:00
|
|
|
want := filepath.Join(wd, "testdata", "rootfind")
|
2017-01-25 10:29:04 +03:00
|
|
|
got1, err := findProjectRoot(want)
|
2016-10-18 18:05:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error while finding root: %s", err)
|
2017-01-25 10:29:04 +03:00
|
|
|
} else if want != got1 {
|
|
|
|
t.Errorf("findProjectRoot directly on root dir should have found %s, got %s", want, got1)
|
2016-10-18 18:05:34 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
got2, err := findProjectRoot(filepath.Join(want, "subdir"))
|
2016-10-18 18:05:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error while finding root: %s", err)
|
2017-01-25 10:29:04 +03:00
|
|
|
} else if want != got2 {
|
|
|
|
t.Errorf("findProjectRoot on subdir should have found %s, got %s", want, got2)
|
2016-10-18 18:05:34 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
got3, err := findProjectRoot(filepath.Join(want, "nonexistent"))
|
2016-10-18 18:05:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error while finding root: %s", err)
|
2017-01-25 10:29:04 +03:00
|
|
|
} else if want != got3 {
|
|
|
|
t.Errorf("findProjectRoot on nonexistent subdir should still work and give %s, got %s", want, got3)
|
2016-10-18 18:05:34 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 04:40:23 +03:00
|
|
|
root := "/"
|
2017-01-25 10:29:04 +03:00
|
|
|
p, err := findProjectRoot(root)
|
|
|
|
if p != "" {
|
|
|
|
t.Errorf("findProjectRoot with path %s returned non empty string: %s", root, p)
|
2017-01-25 04:40:23 +03:00
|
|
|
}
|
2017-01-25 10:29:04 +03:00
|
|
|
if err != errProjectNotFound {
|
|
|
|
t.Errorf("findProjectRoot want: %#v got: %#v", errProjectNotFound, err)
|
2017-01-25 04:40:23 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
// The following test does not work on windows because syscall.Stat does not
|
|
|
|
// return a "not a directory" error.
|
2017-01-14 00:51:15 +03:00
|
|
|
if runtime.GOOS != "windows" {
|
2017-01-27 23:39:07 +03:00
|
|
|
got4, err := findProjectRoot(filepath.Join(want, ManifestName))
|
2017-01-14 00:51:15 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Should have err'd when trying subdir of file, but returned %s", got4)
|
|
|
|
}
|
2016-10-18 18:05:34 +03:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 11:21:20 +03:00
|
|
|
|
2017-09-20 09:00:34 +03:00
|
|
|
func TestCheckGopkgFilenames(t *testing.T) {
|
|
|
|
// We are trying to skip this test on file systems which are case-sensiive. We could
|
|
|
|
// have used `fs.IsCaseSensitiveFilesystem` for this check. However, the code we are
|
|
|
|
// testing also relies on `fs.IsCaseSensitiveFilesystem`. So a bug in
|
|
|
|
// `fs.IsCaseSensitiveFilesystem` could prevent this test from being run. This is the
|
|
|
|
// only scenario where we prefer the OS heuristic over doing the actual work of
|
|
|
|
// validating filesystem case sensitivity via `fs.IsCaseSensitiveFilesystem`.
|
2017-09-02 11:21:20 +03:00
|
|
|
if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
|
|
|
|
t.Skip("skip this test on non-Windows, non-macOS")
|
|
|
|
}
|
|
|
|
|
|
|
|
errMsgFor := func(filetype, filename string) func(string) string {
|
|
|
|
return func(name string) string {
|
2017-09-05 07:38:06 +03:00
|
|
|
return fmt.Sprintf("%s filename %q does not match %q", filetype, name, filename)
|
2017-09-02 11:21:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestErrMsg := errMsgFor("manifest", ManifestName)
|
|
|
|
lockErrMsg := errMsgFor("lock", LockName)
|
|
|
|
|
|
|
|
invalidMfName := strings.ToLower(ManifestName)
|
|
|
|
invalidLfName := strings.ToLower(LockName)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
wantErr bool
|
|
|
|
createFiles []string
|
|
|
|
wantErrMsg string
|
|
|
|
}{
|
2017-09-05 07:38:06 +03:00
|
|
|
// No error should be returned when the project contains a valid manifest file
|
|
|
|
// but no lock file.
|
2017-09-02 11:21:20 +03:00
|
|
|
{false, []string{ManifestName}, ""},
|
2017-09-05 07:38:06 +03:00
|
|
|
// No error should be returned when the project contains a valid manifest file as
|
|
|
|
// well as a valid lock file.
|
2017-09-02 11:21:20 +03:00
|
|
|
{false, []string{ManifestName, LockName}, ""},
|
2017-09-05 07:38:06 +03:00
|
|
|
// Error indicating the project was not found should be returned if a manifest
|
|
|
|
// file is not found.
|
|
|
|
{true, nil, errProjectNotFound.Error()},
|
|
|
|
// Error should be returned if the project has a manifest file with invalid name
|
|
|
|
// but no lock file.
|
2017-09-02 11:21:20 +03:00
|
|
|
{true, []string{invalidMfName}, manifestErrMsg(invalidMfName)},
|
2017-09-05 07:38:06 +03:00
|
|
|
// Error should be returned if the project has a valid manifest file and an
|
|
|
|
// invalid lock file.
|
2017-09-02 11:21:20 +03:00
|
|
|
{true, []string{ManifestName, invalidLfName}, lockErrMsg(invalidLfName)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
h := test.NewHelper(t)
|
|
|
|
defer h.Cleanup()
|
|
|
|
|
2017-09-05 07:38:06 +03:00
|
|
|
// Create a temporary directory which we will use as the project folder.
|
2017-09-02 11:21:20 +03:00
|
|
|
h.TempDir("")
|
|
|
|
tmpPath := h.Path(".")
|
|
|
|
|
2017-09-05 07:38:06 +03:00
|
|
|
// Create any files that are needed for the test before invoking
|
2017-09-20 09:00:34 +03:00
|
|
|
// `checkGopkgFilenames`.
|
2017-09-02 11:21:20 +03:00
|
|
|
for _, file := range c.createFiles {
|
|
|
|
h.TempFile(file, "")
|
|
|
|
}
|
2017-09-20 09:00:34 +03:00
|
|
|
err := checkGopkgFilenames(tmpPath)
|
2017-09-02 11:21:20 +03:00
|
|
|
|
|
|
|
if c.wantErr {
|
|
|
|
if err == nil {
|
2017-09-05 07:38:06 +03:00
|
|
|
// We were expecting an error but did not get one.
|
2017-09-02 11:21:20 +03:00
|
|
|
t.Fatalf("unexpected error message: \n\t(GOT) nil\n\t(WNT) %s", c.wantErrMsg)
|
|
|
|
} else if err.Error() != c.wantErrMsg {
|
2017-09-05 07:38:06 +03:00
|
|
|
// We got an error but it is not the one we were expecting.
|
2017-09-02 11:21:20 +03:00
|
|
|
t.Fatalf("unexpected error message: \n\t(GOT) %s\n\t(WNT) %s", err.Error(), c.wantErrMsg)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
// Error was not expected but still we got one
|
|
|
|
t.Fatalf("unexpected error message: \n\t(GOT) %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 04:40:23 +03:00
|
|
|
|
|
|
|
func TestProjectMakeParams(t *testing.T) {
|
2017-08-08 02:14:55 +03:00
|
|
|
m := NewManifest()
|
|
|
|
m.Ignored = []string{"ignoring this"}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
p := Project{
|
|
|
|
AbsRoot: "someroot",
|
|
|
|
ImportRoot: gps.ProjectRoot("Some project root"),
|
2017-08-08 02:14:55 +03:00
|
|
|
Manifest: m,
|
2017-01-27 23:39:07 +03:00
|
|
|
Lock: &Lock{},
|
2017-01-25 04:40:23 +03:00
|
|
|
}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
solveParam := p.MakeParams()
|
2017-01-25 04:40:23 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
if solveParam.Manifest != p.Manifest {
|
2017-01-25 04:40:23 +03:00
|
|
|
t.Error("makeParams() returned gps.SolveParameters with incorrect Manifest")
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
if solveParam.Lock != p.Lock {
|
2017-01-25 04:40:23 +03:00
|
|
|
t.Error("makeParams() returned gps.SolveParameters with incorrect Lock")
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 04:22:42 +03:00
|
|
|
|
2017-05-11 19:56:14 +03:00
|
|
|
func TestBackupVendor(t *testing.T) {
|
|
|
|
h := test.NewHelper(t)
|
|
|
|
defer h.Cleanup()
|
|
|
|
|
|
|
|
pc := NewTestProjectContext(h, "vendorbackupproject")
|
|
|
|
defer pc.Release()
|
|
|
|
|
|
|
|
dummyFile := filepath.Join("vendor", "badinput_fileroot")
|
|
|
|
pc.CopyFile(dummyFile, "txn_writer/badinput_fileroot")
|
|
|
|
pc.Load()
|
|
|
|
|
|
|
|
if err := pc.VendorShouldExist(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a backup
|
2017-05-20 18:22:09 +03:00
|
|
|
wantName := "_vendor-sfx"
|
2017-05-11 19:56:14 +03:00
|
|
|
vendorbak, err := BackupVendor("vendor", "sfx")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vendorbak != wantName {
|
|
|
|
t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, wantName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pc.h.ShouldExist(vendorbak); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pc.h.ShouldExist(vendorbak + string(filepath.Separator) + "badinput_fileroot"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should return error on creating backup with existing filename
|
|
|
|
vendorbak, err = BackupVendor("vendor", "sfx")
|
|
|
|
|
|
|
|
if err != errVendorBackupFailed {
|
|
|
|
t.Fatalf("Vendor backup error is not as expected: \n\t(GOT) %v\n\t(WNT) %v", err, errVendorBackupFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vendorbak != "" {
|
|
|
|
t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete vendor
|
2017-05-12 16:14:50 +03:00
|
|
|
if err = os.RemoveAll("vendor"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-11 19:56:14 +03:00
|
|
|
|
|
|
|
// Should return empty backup file name when no vendor exists
|
|
|
|
vendorbak, err = BackupVendor("vendor", "sfx")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vendorbak != "" {
|
|
|
|
t.Fatalf("Vendor backup name is not as expected: \n\t(GOT) %v\n\t(WNT) %v", vendorbak, "")
|
|
|
|
}
|
|
|
|
}
|