From ec95d6c72eda65520f1c5e7814dca4a2ea10e9a0 Mon Sep 17 00:00:00 2001 From: wenovus Date: Wed, 29 Nov 2023 19:43:10 -0800 Subject: [PATCH] Add tests and fix major version comment --- post_results/misc-checks.go | 10 ++--- post_results/misc-checks_test.go | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/post_results/misc-checks.go b/post_results/misc-checks.go index 6adef0d..de85638 100644 --- a/post_results/misc-checks.go +++ b/post_results/misc-checks.go @@ -36,18 +36,16 @@ type versionRecord struct { type versionRecordSlice []versionRecord func (s versionRecordSlice) MajorVersionChanges() string { - if len(s) == 0 { - return fmt.Sprintf("No major YANG version changes in commit %s", commitSHA) - } - var b strings.Builder - b.WriteString(fmt.Sprintf("Major YANG version changes in commit %s:\n", commitSHA)) for _, change := range s { if change.OldMajorVersion != change.NewMajorVersion || change.NewVersion == "" { b.WriteString(fmt.Sprintf("%s: `%s` -> `%s`\n", change.File, change.OldVersion, change.NewVersion)) } } - return b.String() + if b.Len() == 0 { + return fmt.Sprintf("No major YANG version changes in commit %s", commitSHA) + } + return fmt.Sprintf("Major YANG version changes in commit %s:\n%s", commitSHA, b.String()) } func (s versionRecordSlice) hasBreaking() bool { diff --git a/post_results/misc-checks_test.go b/post_results/misc-checks_test.go index 47c07a6..0f6568a 100644 --- a/post_results/misc-checks_test.go +++ b/post_results/misc-checks_test.go @@ -77,3 +77,80 @@ func TestHasBreaking(t *testing.T) { }) } } + +func TestMajorVersionChanges(t *testing.T) { + commitSHA = "a0" + tests := []struct { + desc string + inVersions versionRecordSlice + wantMajorVersionChanges string + }{{ + desc: "deleted", + inVersions: versionRecordSlice{{ + File: "openconfig-deleted.yang", + OldMajorVersion: 1, + NewMajorVersion: 0, + OldVersion: "1.0.0", + NewVersion: "", + }}, + wantMajorVersionChanges: "Major YANG version changes in commit a0:\nopenconfig-deleted.yang: `1.0.0` -> ``\n", + }, { + desc: "minor", + inVersions: versionRecordSlice{{ + File: "openconfig-acl-submodule.yang", + OldMajorVersion: 1, + NewMajorVersion: 1, + OldVersion: "1.1.3", + NewVersion: "1.2.3", + }}, + wantMajorVersionChanges: `No major YANG version changes in commit a0`, + }, { + desc: "patch", + inVersions: versionRecordSlice{{ + File: "openconfig-acl.yang", + OldMajorVersion: 1, + NewMajorVersion: 1, + OldVersion: "1.2.2", + NewVersion: "1.2.3", + }}, + wantMajorVersionChanges: `No major YANG version changes in commit a0`, + }, { + desc: "one", + inVersions: versionRecordSlice{{ + File: "openconfig-interface-submodule.yang", + OldMajorVersion: 0, + NewMajorVersion: 1, + OldVersion: "0.5.0", + NewVersion: "1.0.0", + }}, + wantMajorVersionChanges: "Major YANG version changes in commit a0:\nopenconfig-interface-submodule.yang: `0.5.0` -> `1.0.0`\n", + }, { + desc: "major", + inVersions: versionRecordSlice{{ + File: "openconfig-interface.yang", + OldMajorVersion: 1, + NewMajorVersion: 2, + OldVersion: "1.1.3", + NewVersion: "2.0.0", + }}, + wantMajorVersionChanges: "Major YANG version changes in commit a0:\nopenconfig-interface.yang: `1.1.3` -> `2.0.0`\n", + }, { + desc: "minor", + inVersions: versionRecordSlice{{ + File: "openconfig-packet-match.yang", + OldMajorVersion: 1, + NewMajorVersion: 1, + OldVersion: "1.1.2", + NewVersion: "1.2.0", + }}, + wantMajorVersionChanges: `No major YANG version changes in commit a0`, + }} + + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + if got, want := tt.inVersions.MajorVersionChanges(), tt.wantMajorVersionChanges; got != want { + t.Errorf("got %v, want %v", got, want) + } + }) + } +}