func (s *ZipSuite) TestFind(c *gc.C) { reader := s.makeZip(c, ft.File{"some-file", "", 0644}, ft.File{"another-file", "", 0644}, ft.Symlink{"some-symlink", "some-file"}, ft.Dir{"some-dir", 0755}, ft.Dir{"some-dir/another-dir", 0755}, ft.File{"some-dir/another-file", "", 0644}, ) for i, test := range []struct { pattern string expect []string }{{ "", nil, }, { "no-matches", nil, }, { "some-file", []string{ "some-file"}, }, { "another-file", []string{ "another-file", "some-dir/another-file"}, }, { "some-*", []string{ "some-file", "some-symlink", "some-dir"}, }, { "another-*", []string{ "another-file", "some-dir/another-dir", "some-dir/another-file"}, }, { "*", []string{ "some-file", "another-file", "some-symlink", "some-dir", "some-dir/another-dir", "some-dir/another-file"}, }} { c.Logf("test %d: %q", i, test.pattern) actual, err := zip.Find(reader, test.pattern) c.Assert(err, gc.IsNil) sort.Strings(test.expect) sort.Strings(actual) c.Check(actual, jc.DeepEquals, test.expect) } c.Logf("test $spanish-inquisition: FindAll") expect, err := zip.Find(reader, "*") c.Assert(err, gc.IsNil) actual, err := zip.FindAll(reader) c.Assert(err, gc.IsNil) sort.Strings(expect) sort.Strings(actual) c.Check(actual, jc.DeepEquals, expect) }
// findArchiveRootDir scans a zip archive and returns the rootDir of // the archive, the one containing metadata.yaml, config.yaml and // revision files, or an error if the archive appears invalid. func (h *charmsHandler) findArchiveRootDir(zipr *zip.Reader) (string, error) { paths, err := ziputil.Find(zipr, "metadata.yaml") if err != nil { return "", err } switch len(paths) { case 0: return "", fmt.Errorf("invalid charm archive: missing metadata.yaml") case 1: default: sort.Sort(byDepth(paths)) if depth(paths[0]) == depth(paths[1]) { return "", fmt.Errorf("invalid charm archive: ambiguous root directory") } } return filepath.Dir(paths[0]), nil }
func (s *ZipSuite) TestFindError(c *gc.C) { reader := s.makeZip(c, ft.File{"some-file", "", 0644}) _, err := zip.Find(reader, "[]") c.Assert(err, gc.ErrorMatches, "syntax error in pattern") }