all: Add build constants for runtime.GOOS comparisons (#8442)

all: Add package runtimeos for runtime.GOOS comparisons

I grew tired of hand written string comparisons. This adds generated
constants for the GOOS values, and predefined Is$OS constants that can
be iffed on. In a couple of places I rewrote trivial switch:es to if:s,
and added Illumos where we checked for Solaris (because they are
effectively the same, and if we're going to target one of them that
would be Illumos...).
This commit is contained in:
Jakob Borg
2022-07-28 19:36:39 +02:00
committed by GitHub
parent f13d65262a
commit a3c724f2c3
51 changed files with 282 additions and 192 deletions
+2 -2
View File
@@ -11,11 +11,11 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/shirou/gopsutil/v3/disk"
"github.com/syncthing/syncthing/lib/build"
)
var (
@@ -79,7 +79,7 @@ func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem {
// Attempt to enable long filename support on Windows. We may still not
// have an absolute path here if the previous steps failed.
if runtime.GOOS == "windows" {
if build.IsWindows {
root = longFilenameSupport(root)
}
+8 -8
View File
@@ -9,13 +9,13 @@ package fs
import (
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"testing"
"time"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/rand"
)
@@ -55,7 +55,7 @@ func TestChmodFile(t *testing.T) {
}
func TestChownFile(t *testing.T) {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("Not supported on Windows")
return
}
@@ -106,7 +106,7 @@ func TestChmodDir(t *testing.T) {
path := filepath.Join(dir, "dir")
mode := os.FileMode(0755)
if runtime.GOOS == "windows" {
if build.IsWindows {
mode = os.FileMode(0777)
}
@@ -177,7 +177,7 @@ func TestCreate(t *testing.T) {
}
func TestCreateSymlink(t *testing.T) {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("windows not supported")
}
@@ -336,7 +336,7 @@ func TestUsage(t *testing.T) {
fs, _ := setup(t)
usage, err := fs.Usage(".")
if err != nil {
if runtime.GOOS == "netbsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "solaris" {
if build.IsNetBSD || build.IsOpenBSD || build.IsSolaris || build.IsIllumos {
t.Skip()
}
t.Errorf("Unexpected error: %s", err)
@@ -429,7 +429,7 @@ func TestRooted(t *testing.T) {
{"/", ".", "/", true},
}
if runtime.GOOS == "windows" {
if build.IsWindows {
extraCases := []testcase{
{`c:\`, `foo`, `c:\foo`, true},
{`\\?\c:\`, `foo`, `\\?\c:\foo`, true},
@@ -503,7 +503,7 @@ func TestRooted(t *testing.T) {
}
func TestNewBasicFilesystem(t *testing.T) {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("non-windows root paths")
}
@@ -550,7 +550,7 @@ func TestRel(t *testing.T) {
{"/", "/Test", "Test"},
{"/Test", "/Test/test", "test"},
}
if runtime.GOOS == "windows" {
if build.IsWindows {
for i := range testCases {
testCases[i].root = filepath.FromSlash(testCases[i].root)
testCases[i].abs = filepath.FromSlash(testCases[i].abs)
+12 -12
View File
@@ -15,7 +15,6 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
@@ -23,6 +22,7 @@ import (
"time"
"github.com/syncthing/notify"
"github.com/syncthing/syncthing/lib/build"
)
func TestMain(m *testing.M) {
@@ -41,7 +41,7 @@ func TestMain(m *testing.M) {
}
testDirAbs = filepath.Join(dir, testDir)
if runtime.GOOS == "windows" {
if build.IsWindows {
testDirAbs = longFilenameSupport(testDirAbs)
}
@@ -68,7 +68,7 @@ var (
)
func TestWatchIgnore(t *testing.T) {
if runtime.GOOS == "openbsd" {
if build.IsOpenBSD {
t.Skip(failsOnOpenBSD)
}
name := "ignore"
@@ -92,7 +92,7 @@ func TestWatchIgnore(t *testing.T) {
}
func TestWatchInclude(t *testing.T) {
if runtime.GOOS == "openbsd" {
if build.IsOpenBSD {
t.Skip(failsOnOpenBSD)
}
name := "include"
@@ -119,7 +119,7 @@ func TestWatchInclude(t *testing.T) {
}
func TestWatchRename(t *testing.T) {
if runtime.GOOS == "openbsd" {
if build.IsOpenBSD {
t.Skip(failsOnOpenBSD)
}
name := "rename"
@@ -134,7 +134,7 @@ func TestWatchRename(t *testing.T) {
destEvent := Event{new, Remove}
// Only on these platforms the removed file can be differentiated from
// the created file during renaming
if runtime.GOOS == "windows" || runtime.GOOS == "linux" || runtime.GOOS == "solaris" || runtime.GOOS == "freebsd" {
if build.IsWindows || build.IsLinux || build.IsSolaris || build.IsIllumos || build.IsFreeBSD {
destEvent = Event{new, NonRemove}
}
expectedEvents := []Event{
@@ -154,7 +154,7 @@ func TestWatchRename(t *testing.T) {
// out of root event on every event.
// https://github.com/syncthing/syncthing/issues/5695
func TestWatchWinRoot(t *testing.T) {
if runtime.GOOS != "windows" {
if !build.IsWindows {
t.Skip("Windows specific test")
}
@@ -288,7 +288,7 @@ func TestWatchSubpath(t *testing.T) {
// TestWatchOverflow checks that an event at the root is sent when maxFiles is reached
func TestWatchOverflow(t *testing.T) {
if runtime.GOOS == "openbsd" {
if build.IsOpenBSD {
t.Skip(failsOnOpenBSD)
}
name := "overflow"
@@ -313,7 +313,7 @@ func TestWatchOverflow(t *testing.T) {
}
func TestWatchErrorLinuxInterpretation(t *testing.T) {
if runtime.GOOS != "linux" {
if !build.IsLinux {
t.Skip("testing of linux specific error codes")
}
@@ -341,7 +341,7 @@ func TestWatchErrorLinuxInterpretation(t *testing.T) {
}
func TestWatchSymlinkedRoot(t *testing.T) {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("Involves symlinks")
}
@@ -385,7 +385,7 @@ func TestUnrootedChecked(t *testing.T) {
}
func TestWatchIssue4877(t *testing.T) {
if runtime.GOOS != "windows" {
if !build.IsWindows {
t.Skip("Windows specific test")
}
@@ -440,7 +440,7 @@ func TestWatchModTime(t *testing.T) {
var allowedEvents []Event
// Apparently an event for the parent is also sent on mac
if runtime.GOOS == "darwin" {
if build.IsDarwin {
allowedEvents = []Event{
{name, NonRemove},
}
+4 -2
View File
@@ -17,6 +17,8 @@ import (
"sort"
"testing"
"time"
"github.com/syncthing/syncthing/lib/build"
)
func TestFakeFS(t *testing.T) {
@@ -563,7 +565,7 @@ func testFakeFSRenameInsensitive(t *testing.T, fs Filesystem) {
}
// not checking on darwin due to https://github.com/golang/go/issues/35222
if runtime.GOOS != "darwin" {
if !build.IsDarwin {
if err := fs.Rename("/foo/bar/BAZ", "/FOO/BAR/bAz"); err != nil {
t.Errorf("Could not perform in-place case-only directory rename: %s", err)
}
@@ -786,7 +788,7 @@ func testFakeFSSameFile(t *testing.T, fs Filesystem) {
t.Fatalf("Could not create %s: %s", filename, err)
} else {
fd.Close()
if runtime.GOOS == "windows" {
if build.IsWindows {
time.Sleep(1 * time.Millisecond)
}
}
+6 -5
View File
@@ -8,8 +8,9 @@ package fs
import (
"path/filepath"
"runtime"
"testing"
"github.com/syncthing/syncthing/lib/build"
)
func TestIsInternal(t *testing.T) {
@@ -120,7 +121,7 @@ func TestIsParent(t *testing.T) {
testBoth := func(path, parent string, expected bool) {
t.Helper()
test(path, parent, expected)
if runtime.GOOS == "windows" {
if build.IsWindows {
test("C:/"+path, "C:/"+parent, expected)
} else {
test("/"+path, "/"+parent, expected)
@@ -130,7 +131,7 @@ func TestIsParent(t *testing.T) {
// rel - abs
for _, parent := range []string{"/", "/foo", "/foo/bar"} {
for _, path := range []string{"", ".", "foo", "foo/bar", "bas", "bas/baz"} {
if runtime.GOOS == "windows" {
if build.IsWindows {
parent = "C:/" + parent
}
test(parent, path, false)
@@ -140,7 +141,7 @@ func TestIsParent(t *testing.T) {
// equal
for i, path := range []string{"/", "/foo", "/foo/bar", "", ".", "foo", "foo/bar"} {
if i < 3 && runtime.GOOS == "windows" {
if i < 3 && build.IsWindows {
path = "C:" + path
}
test(path, path, false)
@@ -164,7 +165,7 @@ func TestIsParent(t *testing.T) {
for _, path := range []string{"foo/bar/baz", "foo/bar/baz/bas"} {
testBoth(path, parent, true)
testBoth(parent, path, false)
if runtime.GOOS == "windows" {
if build.IsWindows {
test("C:/"+path, "D:/"+parent, false)
}
}
+4 -4
View File
@@ -10,9 +10,10 @@ import (
"errors"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/syncthing/syncthing/lib/build"
)
func TestMtimeFS(t *testing.T) {
@@ -181,11 +182,10 @@ func TestMtimeFSOpen(t *testing.T) {
}
func TestMtimeFSInsensitive(t *testing.T) {
switch runtime.GOOS {
case "darwin", "windows":
if build.IsDarwin || build.IsWindows {
// blatantly assume file systems here are case insensitive. Might be
// a spurious failure on oddly configured systems.
default:
} else {
t.Skip("need case insensitive FS")
}
+2 -2
View File
@@ -9,9 +9,9 @@ package fs
import (
"fmt"
"path/filepath"
"runtime"
"strings"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/sha256"
)
@@ -21,7 +21,7 @@ const (
)
func tempPrefix() string {
if runtime.GOOS == "windows" {
if build.IsWindows {
return WindowsTempPrefix
} else {
return UnixTempPrefix
+4 -3
View File
@@ -10,9 +10,10 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"unicode"
"github.com/syncthing/syncthing/lib/build"
)
const pathSeparatorString = string(PathSeparator)
@@ -35,7 +36,7 @@ func ExpandTilde(path string) (string, error) {
}
func getHomeDir() (string, error) {
if runtime.GOOS == "windows" {
if build.IsWindows {
// Legacy -- we prioritize this for historical reasons, whereas
// os.UserHomeDir uses %USERPROFILE% always.
home := filepath.Join(os.Getenv("HomeDrive"), os.Getenv("HomePath"))
@@ -196,7 +197,7 @@ func CommonPrefix(first, second string) string {
}
if isAbs {
if runtime.GOOS == "windows" && isVolumeNameOnly(common) {
if build.IsWindows && isVolumeNameOnly(common) {
// Because strings.Split strips out path separators, if we're at the volume name, we end up without a separator
// Wedge an empty element to be joined with.
common = append(common, "")
+3 -2
View File
@@ -8,10 +8,11 @@ package fs
import (
"math/rand"
"runtime"
"testing"
"unicode"
"unicode/utf8"
"github.com/syncthing/syncthing/lib/build"
)
func TestCommonPrefix(t *testing.T) {
@@ -23,7 +24,7 @@ func TestCommonPrefix(t *testing.T) {
}
}
if runtime.GOOS == "windows" {
if build.IsWindows {
test(`c:\Audrius\Downloads`, `c:\Audrius\Docs`, `c:\Audrius`)
test(`c:\Audrius\Downloads`, `C:\Audrius\Docs`, ``) // Case differences :(
test(`C:\Audrius-a\Downloads`, `C:\Audrius-b\Docs`, `C:\`)
+5 -4
View File
@@ -11,12 +11,13 @@ import (
"fmt"
osexec "os/exec"
"path/filepath"
"runtime"
"testing"
"github.com/syncthing/syncthing/lib/build"
)
func testWalkSkipSymlink(t *testing.T, fsType FilesystemType, uri string) {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("Symlinks skipping is not tested on windows")
}
@@ -53,7 +54,7 @@ func createDirJunct(target string, name string) error {
}
func testWalkTraverseDirJunct(t *testing.T, fsType FilesystemType, uri string) {
if runtime.GOOS != "windows" {
if !build.IsWindows {
t.Skip("Directory junctions are available and tested on windows only")
}
@@ -86,7 +87,7 @@ func testWalkTraverseDirJunct(t *testing.T, fsType FilesystemType, uri string) {
}
func testWalkInfiniteRecursion(t *testing.T, fsType FilesystemType, uri string) {
if runtime.GOOS != "windows" {
if !build.IsWindows {
t.Skip("Infinite recursion detection is tested on windows only")
}