From eae00773cce87d5282a8ac7c10b5c1961ee6f9cb Mon Sep 17 00:00:00 2001 From: William Bain Date: Thu, 24 Feb 2022 22:21:35 -0500 Subject: Add refspec bindings (#898) This add support for the parse, access, and transform functions for refspec objects. --- refspec_test.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 refspec_test.go (limited to 'refspec_test.go') diff --git a/refspec_test.go b/refspec_test.go new file mode 100644 index 0000000..7064198 --- /dev/null +++ b/refspec_test.go @@ -0,0 +1,75 @@ +package git + +import ( + "testing" +) + +func TestRefspec(t *testing.T) { + t.Parallel() + + const ( + input = "+refs/heads/*:refs/remotes/origin/*" + mainLocal = "refs/heads/main" + mainRemote = "refs/remotes/origin/main" + ) + + refspec, err := ParseRefspec(input, true) + checkFatal(t, err) + + // Accessors + + s := refspec.String() + if s != input { + t.Errorf("expected string %q, got %q", input, s) + } + + if d := refspec.Direction(); d != ConnectDirectionFetch { + t.Errorf("expected fetch refspec, got direction %v", d) + } + + if pat, expected := refspec.Src(), "refs/heads/*"; pat != expected { + t.Errorf("expected refspec src %q, got %q", expected, pat) + } + + if pat, expected := refspec.Dst(), "refs/remotes/origin/*"; pat != expected { + t.Errorf("expected refspec dst %q, got %q", expected, pat) + } + + if !refspec.Force() { + t.Error("expected refspec force flag") + } + + // SrcMatches + + if !refspec.SrcMatches(mainLocal) { + t.Errorf("refspec source did not match %q", mainLocal) + } + + if refspec.SrcMatches("refs/tags/v1.0") { + t.Error("refspec source matched under refs/tags") + } + + // DstMatches + + if !refspec.DstMatches(mainRemote) { + t.Errorf("refspec destination did not match %q", mainRemote) + } + + if refspec.DstMatches("refs/tags/v1.0") { + t.Error("refspec destination matched under refs/tags") + } + + // Transforms + + fromLocal, err := refspec.Transform(mainLocal) + checkFatal(t, err) + if fromLocal != mainRemote { + t.Errorf("transform by refspec returned %s; expected %s", fromLocal, mainRemote) + } + + fromRemote, err := refspec.Rtransform(mainRemote) + checkFatal(t, err) + if fromRemote != mainLocal { + t.Errorf("rtransform by refspec returned %s; expected %s", fromRemote, mainLocal) + } +} -- cgit v1.2.3