FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

test: add unit tests for version-check comparison and skip logic by Montana · Pull Request #1339 · sourcegraph/src-cli · GitHub

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
77 changes: 77 additions & 0 deletions cmd/src/version_check_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"testing"

"github.com/sourcegraph/src-cli/internal/version"
)

func TestIsOutdated(t *testing.T) {
tests := []struct {
name string
current string
recommended string
wantOutdated bool
wantOK bool
}{
{"older patch", "4.3.0", "4.3.1", true, true},
{"older minor", "4.3.0", "4.4.0", true, true},
{"older major", "3.43.0", "4.0.0", true, true},
{"equal", "4.4.0", "4.4.0", false, true},
{"newer", "4.5.0", "4.4.0", false, true},
{"v-prefixed current", "v4.3.0", "4.4.0", true, true},
{"prerelease, same base, not outdated", "4.4.0-rc.1", "4.4.0", false, true},
{"prerelease, older base, outdated", "4.3.0-rc.1", "4.4.0", true, true},
{"unparseable current", "dev", "4.4.0", false, false},
{"empty recommended", "4.3.0", "", false, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOutdated, gotOK := isOutdated(tt.current, tt.recommended)
if gotOutdated != tt.wantOutdated || gotOK != tt.wantOK {
t.Errorf("isOutdated(%q, %q) = (%v, %v), want (%v, %v)",
tt.current, tt.recommended, gotOutdated, gotOK, tt.wantOutdated, tt.wantOK)
}
})
}
}

func TestShouldSkipVersionCheck(t *testing.T) {
// pretend this is a real release build for the duration of the test, since the
// default BuildTag ("dev") would otherwise short-circuit every case.
orig := version.BuildTag
version.BuildTag = "4.3.0"
t.Cleanup(func() { version.BuildTag = orig })

t.Run("skips for no-op subcommands", func(t *testing.T) {
for _, sub := range []string{"", "help", "version", "-h", "--help"} {
if !shouldSkipVersionCheck(sub) {
t.Errorf("shouldSkipVersionCheck(%q) = false, want true", sub)
}
}
})

t.Run("runs for normal subcommands", func(t *testing.T) {
for _, sub := range []string{"search", "batch", "api"} {
if shouldSkipVersionCheck(sub) {
t.Errorf("shouldSkipVersionCheck(%q) = true, want false", sub)
}
}
})

t.Run("skips when opt-out env var is set", func(t *testing.T) {
t.Setenv(envSkipVersionCheck, "1")
if !shouldSkipVersionCheck("search") {
t.Errorf("shouldSkipVersionCheck with %s set = false, want true", envSkipVersionCheck)
}
})

t.Run("skips for dev builds", func(t *testing.T) {
version.BuildTag = version.DefaultBuildTag
defer func() { version.BuildTag = "4.3.0" }()
if !shouldSkipVersionCheck("search") {
t.Error("shouldSkipVersionCheck for dev build = false, want true")
}
})
}

Back | FazBrowse Home | New Git URL