Ejemplo n.º 1
0
// readFiles iterates through tarred files and ensures they are the same.
func readFiles(t *testing.T, f *os.File, tname string, want []file) {
	r := tar.NewReader(f)

	for _, fwant := range want {
		fname, err := r.NextFile()
		switch err {
		case io.EOF:
			return
		default:
			t.Fatalf("%s: read header for next file: %v", tname, err)
		case nil: // Proceed below
		}

		if fname != fwant.name {
			t.Fatalf("%s: incorrect file name: got %q but want %q", tname, fname, fwant.name)
			continue
		}

		gotContents, err := ioutil.ReadAll(r)
		if err != nil {
			t.Fatalf("%s: read file: %v", tname, err)
		}

		if !bytes.Equal(gotContents, fwant.contents) {
			t.Errorf("%s: %q = %q but want %q", tname, fname, gotContents, fwant.contents)
		}
	}
}
Ejemplo n.º 2
0
func reader(t *testing.T, tname string, buf *bytes.Buffer, atype string, fname string) archive.ReadCloser {
	switch atype {
	case gzipType:
		gr, err := gzip.NewReader(buf, fname)
		if err != nil {
			t.Fatalf("%s: %v", tname, err)
		}
		return gr
	case tarType:
		return archive.NopCloser(tar.NewReader(buf))
	case zipType:
		zr, err := zip.NewReader(
			bytes.NewReader(buf.Bytes()),
			int64(buf.Len()))
		if err != nil {
			t.Fatalf("%s: new zip reader: %v", tname, err)
		}
		return archive.NopCloser(zr)
	}
	t.Fatalf("%s: unrecognized archive type: %s", tname, atype)
	panic("execution continued after (*testing.T).Fatalf")
}
Ejemplo n.º 3
0
		handleMultipleEntries: false,
		archiver: func(f *os.File, exploded bool) archive.WriteCloser {
			gw := gzip.NewWriter(f)
			if exploded {
				return gw
			}
			return tar.NewWriteMultiCloser(gw, gw)
		},
		unarchiver: func(f *os.File) (archive.ReadCloser, error) {
			gr, err := gzip.NewReader(f, f.Name())
			if err != nil {
				return nil, err
			}

			// Determine if the gzip is a tar
			tr := tar.NewReader(gr)
			_, err = tr.Next()
			isTar := err == nil

			// Reset to beginning of file
			if _, err := f.Seek(0, os.SEEK_SET); err != nil {
				return nil, err
			}
			gr.Reset(f)

			if isTar {
				return archive.NopCloser(tar.NewReader(gr)), nil
			}
			return gr, nil
		},
	},