| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package htesting |
| 15 |
|
| 16 |
import ( |
| 17 |
"math/rand" |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
"regexp" |
| 21 |
"runtime" |
| 22 |
"strconv" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
qt "github.com/frankban/quicktest" |
| 28 |
|
| 29 |
"github.com/spf13/afero" |
| 30 |
) |
| 31 |
|
| 32 |
// IsTest reports whether we're running as a test. |
| 33 |
var IsTest bool |
| 34 |
|
| 35 |
func init() { |
| 36 |
for _, arg := range os.Args { |
| 37 |
if strings.HasPrefix(arg, "-test.") { |
| 38 |
IsTest = true |
| 39 |
break |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// CreateTempDir creates a temp dir in the given filesystem and |
| 45 |
// returns the dirname and a func that removes it when done. |
| 46 |
func CreateTempDir(fs afero.Fs, prefix string) (string, func(), error) { |
| 47 |
tempDir, err := afero.TempDir(fs, "", prefix) |
| 48 |
if err != nil { |
| 49 |
return "", nil, err |
| 50 |
} |
| 51 |
|
| 52 |
_, isOsFs := fs.(*afero.OsFs) |
| 53 |
|
| 54 |
if isOsFs && runtime.GOOS == "darwin" && !strings.HasPrefix(tempDir, "/private") { |
| 55 |
// To get the entry folder in line with the rest. This its a little bit |
| 56 |
// mysterious, but so be it. |
| 57 |
tempDir = "/private" + tempDir |
| 58 |
} |
| 59 |
return tempDir, func() { fs.RemoveAll(tempDir) }, nil |
| 60 |
} |
| 61 |
|
| 62 |
// IsCaseInsensitiveFs reports whether dir lives on a case-insensitive filesystem |
| 63 |
// (e.g. the default on macOS and Windows). |
| 64 |
func IsCaseInsensitiveFs(dir string) (bool, error) { |
| 65 |
f, err := os.CreateTemp(dir, "case-*.tmp") |
| 66 |
if err != nil { |
| 67 |
return false, err |
| 68 |
} |
| 69 |
f.Close() |
| 70 |
defer os.Remove(f.Name()) |
| 71 |
|
| 72 |
_, err = os.Stat(filepath.Join(dir, strings.ToUpper(filepath.Base(f.Name())))) |
| 73 |
if err != nil { |
| 74 |
if os.IsNotExist(err) { |
| 75 |
return false, nil |
| 76 |
} |
| 77 |
return false, err |
| 78 |
} |
| 79 |
|
| 80 |
return true, nil |
| 81 |
} |
| 82 |
|
| 83 |
// BailOut panics with a stack trace after the given duration. Useful for |
| 84 |
// hanging tests. |
| 85 |
func BailOut(after time.Duration) { |
| 86 |
time.AfterFunc(after, func() { |
| 87 |
buf := make([]byte, 1<<16) |
| 88 |
runtime.Stack(buf, true) |
| 89 |
panic(string(buf)) |
| 90 |
}) |
| 91 |
} |
| 92 |
|
| 93 |
// Rnd is used only for testing. |
| 94 |
var Rnd = rand.New(rand.NewSource(time.Now().UnixNano())) |
| 95 |
|
| 96 |
func RandBool() bool { |
| 97 |
return Rnd.Intn(2) != 0 |
| 98 |
} |
| 99 |
|
| 100 |
// DiffStringSlices returns the difference between two string slices. |
| 101 |
// Useful in tests. |
| 102 |
// See: |
| 103 |
// http://stackoverflow.com/questions/19374219/how-to-find-the-difference-between-two-slices-of-strings-in-golang |
| 104 |
func DiffStringSlices(slice1 []string, slice2 []string) []string { |
| 105 |
diffStr := []string{} |
| 106 |
m := map[string]int{} |
| 107 |
|
| 108 |
for _, s1Val := range slice1 { |
| 109 |
m[s1Val] = 1 |
| 110 |
} |
| 111 |
for _, s2Val := range slice2 { |
| 112 |
m[s2Val] = m[s2Val] + 1 |
| 113 |
} |
| 114 |
|
| 115 |
for mKey, mVal := range m { |
| 116 |
if mVal == 1 { |
| 117 |
diffStr = append(diffStr, mKey) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return diffStr |
| 122 |
} |
| 123 |
|
| 124 |
// DiffStrings splits the strings into fields and runs it into DiffStringSlices. |
| 125 |
// Useful for tests. |
| 126 |
func DiffStrings(s1, s2 string) []string { |
| 127 |
return DiffStringSlices(strings.Fields(s1), strings.Fields(s2)) |
| 128 |
} |
| 129 |
|
| 130 |
// SkipSlowTestUnlessCI skips the test unless we're running in a CI server. |
| 131 |
// Note that you can set CI_LOCAL=1 to run slow tests locally in a CI-like setup. |
| 132 |
func SkipSlowTestUnlessCI(t testing.TB) { |
| 133 |
if !IsCI() { |
| 134 |
t.Skip("skipping slow test in CI") |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// IsCI reports whether we're running in a CI server. |
| 139 |
func IsCI() bool { |
| 140 |
return (os.Getenv("CI") != "" || os.Getenv("CI_LOCAL") != "") && os.Getenv("CIRCLE_BRANCH") == "" |
| 141 |
} |
| 142 |
|
| 143 |
// IsRealCI reports whether we're running in a CI server, but not in a local CI setup. |
| 144 |
func IsRealCI() bool { |
| 145 |
return IsCI() && os.Getenv("CI_LOCAL") == "" |
| 146 |
} |
| 147 |
|
| 148 |
// IsGitHubAction reports whether we're running in a GitHub Action. |
| 149 |
func IsGitHubAction() bool { |
| 150 |
return os.Getenv("GITHUB_ACTION") != "" |
| 151 |
} |
| 152 |
|
| 153 |
// SupportsAll reports whether the running system supports all Hugo features, |
| 154 |
// e.g. AsciiDoc, Pandoc etc. |
| 155 |
func SupportsAll() bool { |
| 156 |
return IsGitHubAction() || os.Getenv("CI_LOCAL") != "" |
| 157 |
} |
| 158 |
|
| 159 |
// GoMinorVersion returns the minor version of the current Go version, |
| 160 |
// e.g. 16 for Go 1.16. |
| 161 |
func GoMinorVersion() int { |
| 162 |
return extractMinorVersionFromGoTag(runtime.Version()) |
| 163 |
} |
| 164 |
|
| 165 |
// IsWindows reports whether this runs on Windows. |
| 166 |
func IsWindows() bool { |
| 167 |
return runtime.GOOS == "windows" |
| 168 |
} |
| 169 |
|
| 170 |
var goMinorVersionRe = regexp.MustCompile(`go1.(\d*)`) |
| 171 |
|
| 172 |
func extractMinorVersionFromGoTag(tag string) int { |
| 173 |
// The tag may be on the form go1.17, go1.17.5 go1.17rc2 -- or just a commit hash. |
| 174 |
match := goMinorVersionRe.FindStringSubmatch(tag) |
| 175 |
|
| 176 |
if len(match) == 2 { |
| 177 |
i, err := strconv.Atoi(match[1]) |
| 178 |
if err != nil { |
| 179 |
return -1 |
| 180 |
} |
| 181 |
return i |
| 182 |
} |
| 183 |
|
| 184 |
// a commit hash, not useful. |
| 185 |
return -1 |
| 186 |
} |
| 187 |
|
| 188 |
// NewPinnedRunner creates a new runner that will only Run tests matching the given regexp. |
| 189 |
// This is added mostly to use in combination with https://marketplace.visualstudio.com/items?itemName=windmilleng.vscode-go-autotest |
| 190 |
func NewPinnedRunner(t testing.TB, pinnedTestRe string) *PinnedRunner { |
| 191 |
if pinnedTestRe == "" { |
| 192 |
pinnedTestRe = ".*" |
| 193 |
} |
| 194 |
pinnedTestRe = strings.ReplaceAll(pinnedTestRe, "_", " ") |
| 195 |
re := regexp.MustCompile("(?i)" + pinnedTestRe) |
| 196 |
return &PinnedRunner{ |
| 197 |
c: qt.New(t), |
| 198 |
re: re, |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
type PinnedRunner struct { |
| 203 |
c *qt.C |
| 204 |
re *regexp.Regexp |
| 205 |
} |
| 206 |
|
| 207 |
func (r *PinnedRunner) Run(name string, f func(c *qt.C)) bool { |
| 208 |
if !r.re.MatchString(name) { |
| 209 |
if IsGitHubAction() { |
| 210 |
r.c.Fatal("found pinned test when running in CI") |
| 211 |
} |
| 212 |
return true |
| 213 |
} |
| 214 |
return r.c.Run(name, f) |
| 215 |
} |