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
+4 -4
View File
@@ -11,13 +11,13 @@ import (
"errors"
"fmt"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"time"
"unicode/utf8"
metrics "github.com/rcrowley/go-metrics"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/ignore"
@@ -470,7 +470,7 @@ func (w *walker) walkDir(ctx context.Context, relPath string, info fs.FileInfo,
func (w *walker) walkSymlink(ctx context.Context, relPath string, info fs.FileInfo, finishedChan chan<- ScanResult) error {
// Symlinks are not supported on Windows. We ignore instead of returning
// an error.
if runtime.GOOS == "windows" {
if build.IsWindows {
return nil
}
@@ -519,7 +519,7 @@ func (w *walker) walkSymlink(ctx context.Context, relPath string, info fs.FileIn
// normalizePath returns the normalized relative path (possibly after fixing
// it on disk), or skip is true.
func (w *walker) normalizePath(path string, info fs.FileInfo) (normPath string, err error) {
if runtime.GOOS == "darwin" {
if build.IsDarwin {
// Mac OS X file names should always be NFD normalized.
normPath = norm.NFD.String(path)
} else {
@@ -579,7 +579,7 @@ func (w *walker) normalizePath(path string, info fs.FileInfo) (normPath string,
// do not depend on type, and things that should be preserved from the
// previous version of the FileInfo.
func (w *walker) updateFileInfo(dst, src protocol.FileInfo) protocol.FileInfo {
if dst.Type == protocol.FileInfoTypeFile && runtime.GOOS == "windows" {
if dst.Type == protocol.FileInfoTypeFile && build.IsWindows {
// If we have an existing index entry, copy the executable bits
// from there.
dst.Permissions |= (src.Permissions & 0111)
+9 -9
View File
@@ -15,13 +15,13 @@ import (
"io"
"os"
"path/filepath"
"runtime"
rdebug "runtime/debug"
"sort"
"sync"
"testing"
"github.com/d4l3k/messagediff"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/ignore"
@@ -178,7 +178,7 @@ func TestVerify(t *testing.T) {
}
func TestNormalization(t *testing.T) {
if runtime.GOOS == "darwin" {
if build.IsDarwin {
t.Skip("Normalization test not possible on darwin")
return
}
@@ -197,7 +197,7 @@ func TestNormalization(t *testing.T) {
}
numInvalid := 2
if runtime.GOOS == "windows" {
if build.IsWindows {
// On Windows, in case 5 the character gets replaced with a
// replacement character \xEF\xBF\xBD at the point it's written to disk,
// which means it suddenly becomes valid (sort of).
@@ -257,7 +257,7 @@ func TestNormalization(t *testing.T) {
func TestNormalizationDarwinCaseFS(t *testing.T) {
// This tests that normalization works on Darwin, through a CaseFS.
if runtime.GOOS != "darwin" {
if !build.IsDarwin {
t.Skip("Normalization test not possible on non-Darwin")
return
}
@@ -321,7 +321,7 @@ func TestIssue1507(_ *testing.T) {
}
func TestWalkSymlinkUnix(t *testing.T) {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("skipping unsupported symlink test")
return
}
@@ -351,7 +351,7 @@ func TestWalkSymlinkUnix(t *testing.T) {
}
func TestWalkSymlinkWindows(t *testing.T) {
if runtime.GOOS != "windows" {
if !build.IsWindows {
t.Skip("skipping unsupported symlink test")
}
@@ -386,7 +386,7 @@ func TestWalkRootSymlink(t *testing.T) {
dest, _ := filepath.Abs("testdata/dir1")
destFs := fs.NewFilesystem(testFsType, dest)
if err := fs.DebugSymlinkForTestsOnly(destFs, testFs, ".", "link"); err != nil {
if runtime.GOOS == "windows" {
if build.IsWindows {
// Probably we require permissions we don't have.
t.Skip("Need admin permissions or developer mode to run symlink test on Windows: " + err.Error())
} else {
@@ -406,7 +406,7 @@ func TestWalkRootSymlink(t *testing.T) {
files = walkDir(testFs, "link", nil, nil, 0)
// Verify that we got the one symlink, except on windows
if runtime.GOOS == "windows" {
if build.IsWindows {
if len(files) != 0 {
t.Errorf("expected no files, not %d", len(files))
}
@@ -588,7 +588,7 @@ func TestScanOwnershipPOSIX(t *testing.T) {
}
func TestScanOwnershipWindows(t *testing.T) {
if runtime.GOOS != "windows" {
if !build.IsWindows {
t.Skip("This test only works on Windows")
}