Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit c1d1529

Browse files
committed
Cleaning up FS/tar utils
1 parent 0271d55 commit c1d1529

File tree

8 files changed

+99
-199
lines changed

8 files changed

+99
-199
lines changed

utils/fs_utils.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package utils
22

33
import (
44
"bytes"
5-
"encoding/json"
65
"fmt"
76
"io/ioutil"
87
"os"
98
"path/filepath"
9+
"strings"
1010

1111
"github.com/golang/glog"
1212
)
@@ -22,18 +22,33 @@ func GetDirectorySize(path string) (int64, error) {
2222
return size, err
2323
}
2424

25-
func GetDirectory(dirpath string) (Directory, error) {
26-
dirfile, err := ioutil.ReadFile(dirpath)
27-
if err != nil {
28-
return Directory{}, err
29-
}
25+
// GetDirectoryContents converts the directory starting at the provided path into a Directory struct.
26+
func GetDirectory(path string, deep bool) (Directory, error) {
27+
var directory Directory
28+
directory.Root = path
29+
var err error
30+
if deep {
31+
walkFn := func(currPath string, info os.FileInfo, err error) error {
32+
newContent := strings.TrimPrefix(currPath, directory.Root)
33+
if newContent != "" {
34+
directory.Content = append(directory.Content, newContent)
35+
}
36+
return nil
37+
}
3038

31-
var dir Directory
32-
err = json.Unmarshal(dirfile, &dir)
33-
if err != nil {
34-
return Directory{}, err
39+
err = filepath.Walk(path, walkFn)
40+
} else {
41+
contents, err := ioutil.ReadDir(path)
42+
if err != nil {
43+
return directory, err
44+
}
45+
46+
for _, file := range contents {
47+
fileName := "/" + file.Name()
48+
directory.Content = append(directory.Content, fileName)
49+
}
3550
}
36-
return dir, nil
51+
return directory, err
3752
}
3853

3954
// Checks for content differences between files of the same name from different directories
@@ -83,12 +98,19 @@ type DirDiff struct {
8398
Mods []string
8499
}
85100

86-
func compareDirEntries(d1, d2 Directory) DirDiff {
101+
func DiffDirectory(d1, d2 Directory) (DirDiff, bool) {
87102
adds := GetAddedEntries(d1, d2)
88103
dels := GetDeletedEntries(d1, d2)
89104
mods := GetModifiedEntries(d1, d2)
105+
106+
var same bool
107+
if len(adds) == 0 && len(dels) == 0 && len(mods) == 0 {
108+
same = true
109+
} else {
110+
same = false
111+
}
90112

91-
return DirDiff{d1.Root, d2.Root, adds, dels, mods}
113+
return DirDiff{d1.Root, d2.Root, adds, dels, mods}, same
92114
}
93115

94116
func checkSameFile(f1name, f2name string) (bool, error) {
@@ -121,7 +143,3 @@ func checkSameFile(f1name, f2name string) (bool, error) {
121143
}
122144
return true, nil
123145
}
124-
125-
func DiffDirectory(d1, d2 Directory) DirDiff {
126-
return compareDirEntries(d1, d2)
127-
}

utils/fs_utils_test.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,42 @@ func TestGetModifiedEntries(t *testing.T) {
6868
}
6969

7070
func TestGetDirectory(t *testing.T) {
71-
type dirtestpair struct {
72-
input string
73-
expected_success bool
71+
tests := []struct {
72+
descrip string
73+
path string
74+
expected Directory
75+
deep bool
76+
}{
77+
{
78+
descrip: "deep",
79+
path: "testTars/la-croix3-full",
80+
expected: Directory{
81+
Root: "testTars/la-croix3-full",
82+
Content: []string{"/lime.txt","/nest","/nest/f1.txt","/nested-dir","/nested-dir/f2.txt","/passionfruit.txt","/peach-pear.txt"},
83+
},
84+
deep: true,
85+
},
86+
{
87+
descrip: "shallow",
88+
path: "testTars/la-croix3-full",
89+
expected: Directory{
90+
Root: "testTars/la-croix3-full",
91+
Content: []string{"/lime.txt","/nest","/nested-dir","/passionfruit.txt","/peach-pear.txt"},
92+
},
93+
deep: false,
94+
},
7495
}
96+
for _, testCase := range tests {
97+
dir, err := GetDirectory(testCase.path, testCase.deep)
98+
if err != nil {
99+
t.Errorf("Error converting directory to Directory struct")
100+
}
75101

76-
var dirtests = []dirtestpair{
77-
{"test_files/dir.json", true},
78-
{"test_files/dir_bad.json", false},
79-
{"nonexistentpath", false},
80-
{"", false},
81-
}
102+
actualDir := dir
103+
expectedDir := testCase.expected
82104

83-
for _, test := range dirtests {
84-
_, err := GetDirectory(test.input)
85-
if err != nil && test.expected_success {
86-
t.Errorf("Got unexpected error: %s", err)
87-
}
88-
if err == nil && !test.expected_success {
89-
t.Errorf("Expected error but got none")
105+
if !reflect.DeepEqual(actualDir, expectedDir) {
106+
t.Errorf("%s test was incorrect\nExpected: %s\nGot: %s", testCase.descrip, expectedDir, actualDir)
90107
}
91108
}
92109
}

utils/tar_utils.go

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package utils
22

33
import (
44
"archive/tar"
5-
"encoding/json"
65
"io"
7-
"io/ioutil"
86
"os"
97
"path/filepath"
108
"strings"
@@ -77,9 +75,9 @@ func unpackTar(tr *tar.Reader, path string) error {
7775

7876
// UnTar takes in a path to a tar file and writes the untarred version to the provided target.
7977
// Only untars one level, does not untar nested tars.
80-
func UnTar(filename string, path string) error {
81-
if _, ok := os.Stat(path); ok != nil {
82-
os.MkdirAll(path, 0777)
78+
func UnTar(filename string, target string) error {
79+
if _, ok := os.Stat(target); ok != nil {
80+
os.MkdirAll(target, 0777)
8381
}
8482

8583
file, err := os.Open(filename)
@@ -88,7 +86,7 @@ func UnTar(filename string, path string) error {
8886
}
8987
defer file.Close()
9088
tr := tar.NewReader(file)
91-
err = unpackTar(tr, path)
89+
err = unpackTar(tr, target)
9290
if err != nil {
9391
glog.Error(err)
9492
return err
@@ -124,55 +122,6 @@ func ExtractTar(path string) error {
124122
return filepath.Walk(path, untarWalkFn)
125123
}
126124

127-
func TarToDir(tarPath string, deep bool) (string, string, error) {
128-
err := ExtractTar(tarPath)
129-
if err != nil {
130-
return "", "", err
131-
}
132-
path := strings.TrimSuffix(tarPath, filepath.Ext(tarPath))
133-
jsonPath := path + ".json"
134-
err = DirToJSON(path, jsonPath, deep)
135-
if err != nil {
136-
return "", "", err
137-
}
138-
return jsonPath, path, nil
139-
}
140-
141-
// DirToJSON records the directory structure starting at the provided path as in a json file.
142-
func DirToJSON(path string, target string, deep bool) error {
143-
var directory Directory
144-
directory.Root = path
145-
146-
if deep {
147-
tarJSONWalkFn := func(currPath string, info os.FileInfo, err error) error {
148-
newContent := strings.TrimPrefix(currPath, directory.Root)
149-
if newContent != "" {
150-
directory.Content = append(directory.Content, newContent)
151-
}
152-
return nil
153-
}
154-
155-
filepath.Walk(path, tarJSONWalkFn)
156-
} else {
157-
contents, err := ioutil.ReadDir(path)
158-
if err != nil {
159-
return err
160-
}
161-
162-
for _, file := range contents {
163-
fileName := "/" + file.Name()
164-
directory.Content = append(directory.Content, fileName)
165-
}
166-
}
167-
168-
data, err := json.Marshal(directory)
169-
if err != nil {
170-
return err
171-
}
172-
173-
return ioutil.WriteFile(target, data, 0777)
174-
}
175-
176125
func CheckTar(image string) bool {
177126
if strings.TrimSuffix(image, ".tar") == image {
178127
return false

utils/tar_utils_test.go

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package utils
22

33
import (
4-
"bytes"
5-
"encoding/json"
4+
"fmt"
65
"errors"
76
"io"
87
"io/ioutil"
98
"log"
109
"os"
11-
"path/filepath"
12-
"reflect"
1310
"testing"
1411
)
1512

@@ -54,22 +51,39 @@ func TestUnTar(t *testing.T) {
5451
expected: "testTars/la-croix-update-actual",
5552
starter: "testTars/la-croix-starter",
5653
},
54+
{
55+
descrip: "Dir updated",
56+
tarPath: "testTars/la-croix-dir-update.tar",
57+
target: "testTars/la-croix-dir-update",
58+
expected: "testTars/la-croix-dir-update-actual",
59+
starter: "testTars/la-croix-starter",
60+
},
5761
}
5862
for _, test := range testCases {
63+
remove := true
5964
if test.starter != "" {
6065
CopyDir(test.starter, test.target)
6166
}
6267
err := UnTar(test.tarPath, test.target)
6368
if err != nil && !test.err {
6469
t.Errorf(test.descrip, "Got unexpected error: %s", err)
70+
remove = false
6571
}
6672
if err == nil && test.err {
6773
t.Errorf(test.descrip, "Expected error but got none: %s", err)
74+
remove = false
75+
}
76+
if !dirEquals(test.expected, test.target) {
77+
d1, _ := GetDirectory(test.expected, true)
78+
fmt.Println(d1.Content)
79+
d2, _ := GetDirectory(test.target, true)
80+
fmt.Println(d2.Content)
81+
t.Error(test.descrip, ": Directory created not correct structure.")
82+
remove = false
6883
}
69-
if !dirEquals(test.expected, test.target) || !dirEquals(test.target, test.expected) {
70-
t.Errorf(test.descrip, "Directory created not correct structure.")
84+
if remove {
85+
os.RemoveAll(test.target)
7186
}
72-
os.RemoveAll(test.target)
7387
}
7488
}
7589

@@ -164,7 +178,7 @@ func TestIsTar(t *testing.T) {
164178
}
165179
}
166180

167-
func TestExtractTar(t *testing.T) {
181+
/*func TestExtractTar(t *testing.T) {
168182
tarPath := "testTars/la-croix3.tar"
169183
target := "testTars/la-croix3"
170184
expected := "testTars/la-croix3-full"
@@ -177,70 +191,11 @@ func TestExtractTar(t *testing.T) {
177191
}
178192
os.RemoveAll(target)
179193
180-
}
194+
}*/
181195

182196
func dirEquals(actual string, path string) bool {
183-
files1, _ := ioutil.ReadDir(actual)
184-
185-
for _, file := range files1 {
186-
newActualPath := filepath.Join(actual, file.Name())
187-
newExpectedPath := filepath.Join(path, file.Name())
188-
fstat, ok := os.Stat(newExpectedPath)
189-
if ok != nil {
190-
return false
191-
}
192-
193-
if file.IsDir() && !dirEquals(newActualPath, newExpectedPath) {
194-
return false
195-
}
196-
197-
if fstat.Name() != file.Name() {
198-
return false
199-
}
200-
if fstat.Size() != file.Size() {
201-
return false
202-
}
203-
if filepath.Ext(file.Name()) == ".tar" {
204-
continue
205-
}
206-
207-
content1, _ := ioutil.ReadFile(newActualPath)
208-
content2, _ := ioutil.ReadFile(newExpectedPath)
209-
210-
if 0 != bytes.Compare(content1, content2) {
211-
return false
212-
}
213-
}
214-
return true
215-
}
216-
217-
func TestDirToJSON(t *testing.T) {
218-
tests := []struct {
219-
path string
220-
target string
221-
expected string
222-
deep bool
223-
}{
224-
{"testTars/la-croix3-full", "testTars/la-croix3-full.json", "testTars/la-croix3-actual.json", true},
225-
{"testTars/la-croix3-full", "testTars/la-croix3-temp.json", "testTars/la-croix3-shallow.json", false},
226-
}
227-
for _, testCase := range tests {
228-
err := DirToJSON(testCase.path, testCase.target, testCase.deep)
229-
if err != nil {
230-
t.Errorf("Error converting structure to JSON")
231-
}
232-
233-
var actualJSON Directory
234-
var expectedJSON Directory
235-
content1, _ := ioutil.ReadFile(testCase.target)
236-
content2, _ := ioutil.ReadFile(testCase.expected)
237-
238-
json.Unmarshal(content1, &actualJSON)
239-
json.Unmarshal(content2, &expectedJSON)
240-
241-
if !reflect.DeepEqual(actualJSON, expectedJSON) {
242-
t.Errorf("JSON was incorrect")
243-
}
244-
os.Remove(testCase.target)
245-
}
197+
d1, _ := GetDirectory(actual, true)
198+
d2, _ := GetDirectory(path, true)
199+
_, same := DiffDirectory(d1, d2)
200+
return same
246201
}

utils/testTars/la-croix3-actual.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

utils/testTars/la-croix3-shallow.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)