2021-08-10 00:31:29 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-11-29 04:08:35 +00:00
|
|
|
package version_test
|
2021-08-10 00:31:29 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-20 21:35:19 +00:00
|
|
|
"flag"
|
2021-08-10 00:31:29 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2021-09-03 03:11:20 +00:00
|
|
|
"runtime"
|
2021-08-10 00:31:29 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-11-29 04:08:35 +00:00
|
|
|
|
|
|
|
"tailscale.com/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
findModuleInfo = version.ExportFindModuleInfo
|
|
|
|
cmdName = version.ExportCmdName
|
2021-08-10 00:31:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindModuleInfo(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
name := filepath.Join(dir, "tailscaled-version-test")
|
2022-12-09 22:05:21 +00:00
|
|
|
out, err := exec.Command("go", "build", "-o", name, "tailscale.com/cmd/tailscaled").CombinedOutput()
|
2021-08-10 00:31:29 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to build tailscaled: %v\n%s", err, out)
|
|
|
|
}
|
|
|
|
modinfo, err := findModuleInfo(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
prefix := "path\ttailscale.com/cmd/tailscaled\nmod\ttailscale.com"
|
|
|
|
if !strings.HasPrefix(modinfo, prefix) {
|
|
|
|
t.Errorf("unexpected modinfo contents %q", modinfo)
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 03:11:20 +00:00
|
|
|
|
|
|
|
func exe() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return ".exe"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-09-20 21:35:19 +00:00
|
|
|
|
|
|
|
var findModuleInfoName = flag.String("module-info-file", "", "if non-empty, test findModuleInfo against this filename")
|
|
|
|
|
|
|
|
func TestFindModuleInfoManual(t *testing.T) {
|
|
|
|
exe := *findModuleInfoName
|
|
|
|
if exe == "" {
|
|
|
|
t.Skip("skipping without --module-info-file filename")
|
|
|
|
}
|
|
|
|
cmd := cmdName(exe)
|
|
|
|
mod, err := findModuleInfo(exe)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Logf("Got %q from: %s", cmd, mod)
|
|
|
|
}
|