Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: common/archive/ar/ar_test.go

Issue 2014243002: WIP: Archive command which is much faster (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Fixes. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package ar
6
7 import (
8 "bytes"
9 "github.com/maruel/ut"
10 "io/ioutil"
11 "os"
12 "os/exec"
13 "path"
14 "strings"
15 "testing"
16 "time"
17 )
18
19 var (
20 TestFile1 = ("" +
21 // ar file header
22 "!<arch>\n" +
23 // filename len - 16 bytes
24 "#1/9 " +
25 // modtime - 12 bytes
26 "1447140471 " +
27 // owner id - 6 bytes
28 "1000 " +
29 // group id - 6 bytes
30 "1000 " +
31 // file mode - 8 bytes
32 "100640 " +
33 // Data size - 10 bytes
34 "15 " +
35 // File magic - 2 bytes
36 "\x60\n" +
37 // File name - 9 bytes
38 "filename1" +
39 // File data - 6 bytes
40 "abc123" +
41 // Padding - 1 byte
42 "\n" +
43 "")
44 )
45
46 func TestWriterCreatesTestFile1(t *testing.T) {
47 b := &bytes.Buffer{}
48 data := []byte("abc123")
49
50 ar := NewWriter(b)
51 if err := ar.Add("filename1", data); err != nil {
52 t.Fatalf("Add: %v", err)
53 }
54 if err := ar.Close(); err != nil {
55 t.Fatalf("Close: %v", err)
56 }
57
58 ut.AssertEqual(t, []byte(TestFile1), b.Bytes())
59 }
60
61 func TestReaderOnTestFile1(t *testing.T) {
62 r := strings.NewReader(TestFile1)
63
64 ar, err := NewReader(r)
65 if err != nil {
66 t.Fatalf("NewReader: %v", err)
67 }
68
69 h, err := ar.Next()
70 if err != nil {
71 t.Fatalf("Header: %v", err)
72 }
73
74 ut.AssertEqual(t, "filename1", h.Name())
75 ut.AssertEqual(t, int64(6), h.Size())
76 ut.AssertEqual(t, time.Unix(1447140471, 0), h.ModTime())
77
78 data := make([]byte, 6)
79 n, err := ar.Read(data)
80 if err != nil {
81 t.Fatalf("Data: %v", err)
82 }
83 ut.AssertEqual(t, 6, n)
84 ut.AssertEqual(t, []byte("abc123"), data)
85
86 if err := ar.Close(); err != nil {
87 t.Fatalf("Close: %v", err)
88 }
89 }
90
91 func TestWithSystemArCommandList(t *testing.T) {
92 if _, err := exec.LookPath("ar"); err != nil {
93 t.Skipf("ar command not found: %v", err)
94 }
95
96 // Write out to an archive file
97 tmpfile, err := ioutil.TempFile("", "go-ar-test.")
98 if err != nil {
99 t.Fatalf("unable to create temp file: %v", err)
100 }
101 defer os.Remove(tmpfile.Name()) // clean up
102 ar := NewWriter(tmpfile)
103 ar.Add("file1.txt", []byte("file1 contents"))
104 ar.Add("file2.txt", []byte("file2 contents"))
105 ar.Add("dir1/file3.txt", []byte("file3 contents"))
106 ar.Close()
107
108 // Use the ar command to list the file
109 cmd_list := exec.Command("ar", "t", tmpfile.Name())
110 var cmd_list_out_buf bytes.Buffer
111 cmd_list.Stdout = &cmd_list_out_buf
112 if err := cmd_list.Run(); err != nil {
113 t.Fatalf("ar command failed: %v\n%s", err, cmd_list_out_buf.Stri ng())
114 }
115
116 cmd_list_actual_out := cmd_list_out_buf.String()
117 cmd_list_expect_out := `file1.txt
118 file2.txt
119 dir1/file3.txt
120 `
121 ut.AssertEqual(t, cmd_list_expect_out, cmd_list_actual_out)
122 }
123
124 func TestWithSystemArCommandExtract(t *testing.T) {
125 arpath, err := exec.LookPath("ar")
126 if err != nil {
127 t.Skipf("ar command not found: %v", err)
128 }
129
130 // Write out to an archive file
131 tmpfile, err := ioutil.TempFile("", "go-ar-test.")
132 if err != nil {
133 t.Fatalf("unable to create temp file: %v", err)
134 }
135 defer os.Remove(tmpfile.Name()) // clean up
136 ar := NewWriter(tmpfile)
137 ar.Add("file1.txt", []byte("file1 contents"))
138 ar.Add("file2.txt", []byte("file2 contents"))
139 ar.Close()
140
141 // Extract the ar
142 tmpdir, err := ioutil.TempDir("", "go-ar-test.")
143 defer os.RemoveAll(tmpdir)
144 cmd_extract := exec.Cmd{
145 Path: arpath,
146 Args: []string{"ar", "x", tmpfile.Name()},
147 Dir: tmpdir,
148 }
149 var cmd_extract_out_buf bytes.Buffer
150 cmd_extract.Stdout = &cmd_extract_out_buf
151 if err := cmd_extract.Run(); err != nil {
152 t.Fatalf("ar command failed: %v\n%s", err, cmd_extract_out_buf.S tring())
153 }
154
155 // Compare the directory output
156 dir_contents, err := ioutil.ReadDir(tmpdir)
157 if err != nil {
158 t.Fatalf("Unable to read the output directory: %v", err)
159 }
160 for _, fi := range dir_contents {
161 if fi.Name() != "file1.txt" && fi.Name() != "file2.txt" {
162 t.Errorf("Found unexpected file '%s'", fi.Name())
163 }
164 }
165
166 file1_contents, err := ioutil.ReadFile(path.Join(tmpdir, "file1.txt"))
167 file1_expected := []byte("file1 contents")
168 if err != nil {
169 t.Errorf("%v", err)
170 } else {
171 if bytes.Compare(file1_contents, file1_expected) != 0 {
172 t.Errorf("file1.txt content incorrect. Got:\n%v\n%v\n", file1_contents, file1_expected)
173 }
174 }
175
176 file2_contents, err := ioutil.ReadFile(path.Join(tmpdir, "file2.txt"))
177 file2_expected := []byte("file2 contents")
178 if err != nil {
179 t.Errorf("%v", err)
180 } else {
181 if bytes.Compare(file2_contents, file2_expected) != 0 {
182 t.Errorf("file2.txt content incorrect. Got:\n%v\n%v\n", file2_contents, file2_expected)
183 }
184 }
185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698