Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
gps: handle symlinks properly
Browse files Browse the repository at this point in the history
We now delete anything that looks like a symlink if its called
"vendor" while pruning.

Hopefully, you didn't make a bad decision by relying on the magical
properties of symlinks.

Signed-off-by: Ibrahim AshShohail <ibra.sho@gmail.com>
  • Loading branch information
ibrasho committed Dec 10, 2017
1 parent 07a486b commit b50a8b5
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 230 deletions.
8 changes: 2 additions & 6 deletions gps/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ func deriveFilesystemState(root string) (filesystemState, error) {
l := fsLink{path: relPath}

l.to, err = filepath.EvalSymlinks(path)
if strings.HasSuffix(err.Error(), "too many links") {
if err != nil && strings.HasSuffix(err.Error(), "too many links") {
l.circular = true
} else if err != nil {
return err
}

if _, err := os.Stat(l.to); os.IsNotExist(err) {
} else if err != nil && os.IsNotExist(err) {
l.broken = true
} else if err != nil {
return err
Expand Down
106 changes: 100 additions & 6 deletions gps/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gps

import (
"fmt"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -99,28 +100,121 @@ func (tc fsTestCase) setup(t *testing.T) {

func TestDeriveFilesystemState(t *testing.T) {
testcases := []struct {
name string
state filesystemState
name string
fs fsTestCase
}{
{
name: "simple-case",
state: filesystemState{},
name: "simple-case",
fs: fsTestCase{
before: filesystemState{
dirs: []string{
"simple-dir",
},
files: []string{
"simple-file",
},
},
after: filesystemState{
dirs: []string{
"simple-dir",
},
files: []string{
"simple-file",
},
},
},
},
{
name: "simple-symlink-case",
fs: fsTestCase{
before: filesystemState{
dirs: []string{
"simple-dir",
},
files: []string{
"simple-file",
},
links: []fsLink{
fsLink{
path: "link",
to: "nonexisting",
broken: true,
},
},
},
after: filesystemState{
dirs: []string{
"simple-dir",
},
files: []string{
"simple-file",
},
links: []fsLink{
fsLink{
path: "link",
to: "",
broken: true,
},
},
},
},
},
{
name: "complex-symlink-case",
fs: fsTestCase{
before: filesystemState{
links: []fsLink{
fsLink{
path: "link1",
to: "link2",
circular: true,
},
fsLink{
path: "link2",
to: "link1",
circular: true,
},
},
},
after: filesystemState{
links: []fsLink{
fsLink{
path: "link1",
to: "",
circular: true,
},
fsLink{
path: "link2",
to: "",
circular: true,
},
},
},
},
},
}

for _, tc := range testcases {
h := test.NewHelper(t)
defer h.Cleanup()

h.TempDir(tc.name)

tc.fs.before.root = h.Path(tc.name)
tc.fs.after.root = h.Path(tc.name)

tc.fs.setup(t)

state, err := deriveFilesystemState(h.Path(tc.name))
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(tc.state, state) {
if !reflect.DeepEqual(tc.fs.after, state) {
fmt.Println(tc.fs.after)
fmt.Println(state)
t.Fatal("filesystem state mismatch")
}

h.Cleanup()
}
}
Loading

0 comments on commit b50a8b5

Please sign in to comment.