Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "io/ioutil" | |
| 10 "os" | |
| 11 "os/exec" | |
| 12 "path" | |
| 13 "strings" | |
| 14 "testing" | |
| 15 "time" | |
| 16 ) | |
| 17 | |
| 18 var ( | |
| 19 TestFile1 = ("" + | |
| 20 // ar file header | |
| 21 "!<arch>\n" + | |
| 22 // filename len - 16 bytes | |
| 23 "#1/9 " + | |
| 24 // modtime - 12 bytes | |
| 25 "1447140471 " + | |
| 26 // owner id - 6 bytes | |
| 27 "1000 " + | |
| 28 // group id - 6 bytes | |
| 29 "1000 " + | |
| 30 // file mode - 8 bytes | |
| 31 "100640 " + | |
| 32 // Data size - 10 bytes | |
| 33 "15 " + | |
| 34 // File magic - 2 bytes | |
| 35 "\x60\n" + | |
| 36 // File name - 9 bytes | |
| 37 "filename1" + | |
| 38 // File data - 6 bytes | |
| 39 "abc123" + | |
| 40 // Padding - 1 byte | |
| 41 "\n" + | |
| 42 "") | |
| 43 ) | |
| 44 | |
| 45 func TestWriterCreatesTestFile1(t *testing.T) { | |
| 46 b := bytes.NewBufferString("") | |
|
M-A Ruel
2016/06/06 23:01:05
b := &bytes.Buffer{}
mithro
2016/06/07 12:28:52
Done.
| |
| 47 data := []byte("abc123") | |
| 48 | |
| 49 ar := NewWriter(b) | |
| 50 err := ar.Add("filename1", data) | |
|
M-A Ruel
2016/06/06 23:01:05
if err := ar.Add("filename1", data); err != nil {
mithro
2016/06/07 12:28:52
Done.
| |
| 51 if err != nil { | |
| 52 t.Fatalf("Add: %v", err) | |
| 53 } | |
| 54 err = ar.Close() | |
| 55 if err != nil { | |
|
M-A Ruel
2016/06/06 23:01:05
if err := ar.Close(); err != nil {
mithro
2016/06/07 12:28:52
Done.
| |
| 56 t.Fatalf("Close: %v", err) | |
| 57 } | |
| 58 | |
| 59 if g, e := b.Bytes(), []byte(TestFile1); !bytes.Equal(g, e) { | |
| 60 t.Errorf("\ndata = \n%v\n%v", g, e) | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 func TestReaderOnTestFile1(t *testing.T) { | |
| 65 r := strings.NewReader(TestFile1) | |
| 66 | |
| 67 ar, err := NewReader(r) | |
| 68 if err != nil { | |
| 69 t.Fatalf("NewReader: %v", err) | |
| 70 } | |
| 71 | |
| 72 h, err := ar.Next() | |
| 73 if err != nil { | |
| 74 t.Fatalf("Header: %v", err) | |
| 75 } | |
| 76 if g, e := h.Name(), "filename1"; g != e { | |
|
M-A Ruel
2016/06/06 23:01:05
FTR, you can use:
ut.AssertEqual(t, "filename1",
mithro
2016/06/07 12:28:52
It seems that ut is your module? github.com/maruel
M-A Ruel
2016/06/08 20:25:31
Yes, goimports will find it automatically, it's re
mithro
2016/06/10 10:09:52
I was surprised there is no "unittest" like librar
M-A Ruel
2016/06/10 17:51:09
You need to setup $GOPATH locally, get the non goo
Vadim Sh.
2016/06/10 18:00:10
Non-hardcode gophers on chrome-infra are using thi
| |
| 77 t.Errorf("Name() = %q; want %q", g, e) | |
| 78 } | |
| 79 if g, e := h.Size(), int64(6); g != e { | |
| 80 t.Errorf("Size() = %d; want %d", g, e) | |
| 81 } | |
| 82 if g, e := h.ModTime(), time.Unix(1447140471, 0); !g.Equal(e) { | |
| 83 t.Errorf("ModTime() = %v; want %v", g, e) | |
| 84 } | |
| 85 | |
| 86 data := make([]byte, 6) | |
| 87 n, err := ar.Read(data) | |
| 88 if err != nil { | |
| 89 t.Fatalf("Data: %v", err) | |
| 90 } | |
| 91 if g, e := n, 6; g != e { | |
| 92 t.Errorf("n = %v; want %v", g, e) | |
| 93 } | |
| 94 if g, e := data, []byte("abc123"); !bytes.Equal(g, e) { | |
| 95 t.Errorf("data = %v; want %v", g, e) | |
| 96 } | |
| 97 | |
| 98 err = ar.Close() | |
| 99 if err != nil { | |
| 100 t.Fatalf("Close: %v", err) | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 func TestWithSystemArCommandList(t *testing.T) { | |
| 105 _, err := exec.LookPath("ar") | |
|
M-A Ruel
2016/06/06 23:01:05
you can merge here as one line too and a few other
mithro
2016/06/07 12:28:52
I've merged the ones which just return err, but it
M-A Ruel
2016/06/10 17:51:09
Exact
| |
| 106 if err != nil { | |
| 107 t.Skipf("ar command not found: %v", err) | |
| 108 } | |
| 109 | |
| 110 // Write out to an archive file | |
| 111 tmpfile, err := ioutil.TempFile("", "go-ar-test.") | |
| 112 defer os.Remove(tmpfile.Name()) // clean up | |
| 113 ar := NewWriter(tmpfile) | |
| 114 ar.Add("file1.txt", []byte("file1 contents")) | |
| 115 ar.Add("file2.txt", []byte("file2 contents")) | |
| 116 ar.Add("dir1/file3.txt", []byte("file3 contents")) | |
| 117 ar.Close() | |
| 118 | |
| 119 // Use the ar command to list the file | |
| 120 cmd_list := exec.Command("ar", "t", tmpfile.Name()) | |
| 121 var cmd_list_out_buf bytes.Buffer | |
| 122 cmd_list.Stdout = &cmd_list_out_buf | |
| 123 err = cmd_list.Run() | |
| 124 if err != nil { | |
| 125 t.Fatalf("ar command failed: %v\n%s", err, cmd_list_out_buf.Stri ng()) | |
| 126 } | |
| 127 | |
| 128 cmd_list_actual_out := cmd_list_out_buf.String() | |
| 129 cmd_list_expect_out := `file1.txt | |
| 130 file2.txt | |
| 131 dir1/file3.txt | |
| 132 ` | |
| 133 if strings.Compare(cmd_list_actual_out, cmd_list_expect_out) != 0 { | |
| 134 t.Fatalf("ar command output: '%s'", cmd_list_actual_out) | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 func TestWithSystemArCommandExtract(t *testing.T) { | |
| 139 arpath, err := exec.LookPath("ar") | |
| 140 if err != nil { | |
| 141 t.Skipf("ar command not found: %v", err) | |
| 142 } | |
| 143 | |
| 144 // Write out to an archive file | |
| 145 tmpfile, err := ioutil.TempFile("", "go-ar-test.") | |
| 146 defer os.Remove(tmpfile.Name()) // clean up | |
| 147 ar := NewWriter(tmpfile) | |
| 148 ar.Add("file1.txt", []byte("file1 contents")) | |
| 149 ar.Add("file2.txt", []byte("file2 contents")) | |
| 150 ar.Close() | |
| 151 | |
| 152 // Extract the ar | |
| 153 tmpdir, err := ioutil.TempDir("", "go-ar-test.") | |
| 154 defer os.RemoveAll(tmpdir) | |
| 155 cmd_extract := exec.Cmd{ | |
| 156 Path: arpath, | |
| 157 Args: []string{"ar", "x", tmpfile.Name()}, | |
| 158 Dir: tmpdir, | |
| 159 } | |
| 160 err = cmd_extract.Run() | |
| 161 var cmd_extract_out_buf bytes.Buffer | |
| 162 cmd_extract.Stdout = &cmd_extract_out_buf | |
| 163 if err != nil { | |
| 164 t.Fatalf("ar command failed: %v\n%s", err, cmd_extract_out_buf.S tring()) | |
| 165 } | |
| 166 | |
| 167 // Compare the directory output | |
| 168 dir_contents, err := ioutil.ReadDir(tmpdir) | |
| 169 if err != nil { | |
| 170 t.Fatalf("Unable to read the output directory: %v", err) | |
| 171 } | |
| 172 for _, fi := range dir_contents { | |
| 173 if fi.Name() != "file1.txt" && fi.Name() != "file2.txt" { | |
| 174 t.Errorf("Found unexpected file '%s'", fi.Name()) | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 file1_contents, err := ioutil.ReadFile(path.Join(tmpdir, "file1.txt")) | |
| 179 file1_expected := []byte("file1 contents") | |
| 180 if err != nil { | |
| 181 t.Errorf("%v", err) | |
| 182 } else { | |
| 183 if bytes.Compare(file1_contents, file1_expected) != 0 { | |
| 184 t.Errorf("file1.txt content incorrect. Got:\n%v\n%v\n", file1_contents, file1_expected) | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 file2_contents, err := ioutil.ReadFile(path.Join(tmpdir, "file2.txt")) | |
| 189 file2_expected := []byte("file2 contents") | |
| 190 if err != nil { | |
| 191 t.Errorf("%v", err) | |
| 192 } else { | |
| 193 if bytes.Compare(file2_contents, file2_expected) != 0 { | |
| 194 t.Errorf("file2.txt content incorrect. Got:\n%v\n%v\n", file2_contents, file2_expected) | |
| 195 } | |
| 196 } | |
| 197 } | |
| OLD | NEW |