From c50b93a32df1c9d700e3e80996845bc2e13be848 Mon Sep 17 00:00:00 2001 From: Tim Waugh Date: Tue, 26 Mar 2019 16:31:04 +0000 Subject: [PATCH] Add simple test case for DescribeProject Lots of code paths within that function are still not tested. Signed-off-by: Tim Waugh --- retrodep/vendored_test.go | 81 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/retrodep/vendored_test.go b/retrodep/vendored_test.go index 4773bee6..a4d29be9 100644 --- a/retrodep/vendored_test.go +++ b/retrodep/vendored_test.go @@ -1,4 +1,4 @@ -// Copyright (C) 2018 Tim Waugh +// Copyright (C) 2018, 2019 Tim Waugh // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -61,3 +61,82 @@ func TestChooseBestTag(t *testing.T) { t.Errorf("wrong best tag (%s)", best) } } + +type dummyHasher struct{} + +func (h *dummyHasher) Hash(abs, rel string) (FileHash, error) { + return "foo", nil +} + +type mockVendorWorkingTree struct { + stubWorkingTree + + localHashes FileHashes +} + +const matchVersion = "v1.0.0" +const matchRevision = "0123456789abcdef" + +func (wt *mockVendorWorkingTree) FileHashesFromRef(ref, _ string) (FileHashes, error) { + if ref == matchRevision || ref == matchVersion { + // Pretend v1.0.0 is an exact copy of the local files. + return wt.localHashes, nil + } + + // Pretend all other refs have no content at all. + return make(FileHashes), nil +} + +func (wt *mockVendorWorkingTree) RevisionFromTag(tag string) (string, error) { + if tag != matchVersion { + return "", ErrorVersionNotFound + } + return matchRevision, nil +} + +func (wt *mockVendorWorkingTree) ReachableTag(rev string) (tag string, err error) { + if rev == matchVersion { + tag = rev + } else { + err = ErrorVersionNotFound + } + return +} + +func (wt *mockVendorWorkingTree) VersionTags() ([]string, error) { + return []string{"v2.0.0", "v1.0.0"}, nil +} + +func TestDescribeProject(t *testing.T) { + src, err := NewGoSource("testdata/gosource", nil) + if err != nil { + t.Fatal(err) + } + + proj, err := src.Project("github.com/foo/bar") + if err != nil { + t.Fatal(err) + } + + wt := &mockVendorWorkingTree{} + wt.hasher = &dummyHasher{} + + // Make a copy of the local file hashes, so we can mock them + // for "v1.0.0" in the working tree. + wt.localHashes, err = src.hashLocalFiles(wt, proj, src.Path) + if err != nil { + t.Fatal(err) + } + + ref, err := src.DescribeProject(proj, wt, src.Path, nil) + if err != nil { + t.Fatal(err) + } + + if ref.Ver != matchVersion { + t.Errorf("Version: got %s but expected %s", ref.Ver, matchVersion) + } + if ref.Rev != matchRevision { + t.Errorf("Revision: got %s but expected %s", ref.Rev, matchRevision) + } +}