github.com/gohugoio/hugo@v0.88.1/htesting/test_helpers.go (about)

     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  	"regexp"
    20  	"runtime"
    21  	"strconv"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/spf13/afero"
    26  )
    27  
    28  // CreateTempDir creates a temp dir in the given filesystem and
    29  // returns the dirnam and a func that removes it when done.
    30  func CreateTempDir(fs afero.Fs, prefix string) (string, func(), error) {
    31  	tempDir, err := afero.TempDir(fs, "", prefix)
    32  	if err != nil {
    33  		return "", nil, err
    34  	}
    35  
    36  	_, isOsFs := fs.(*afero.OsFs)
    37  
    38  	if isOsFs && runtime.GOOS == "darwin" && !strings.HasPrefix(tempDir, "/private") {
    39  		// To get the entry folder in line with the rest. This its a little bit
    40  		// mysterious, but so be it.
    41  		tempDir = "/private" + tempDir
    42  	}
    43  	return tempDir, func() { fs.RemoveAll(tempDir) }, nil
    44  }
    45  
    46  // BailOut panics with a stack trace after the given duration. Useful for
    47  // hanging tests.
    48  func BailOut(after time.Duration) {
    49  	time.AfterFunc(after, func() {
    50  		buf := make([]byte, 1<<16)
    51  		runtime.Stack(buf, true)
    52  		panic(string(buf))
    53  	})
    54  }
    55  
    56  // Rnd is used only for testing.
    57  var Rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
    58  
    59  func RandBool() bool {
    60  	return Rnd.Intn(2) != 0
    61  }
    62  
    63  // DiffStringSlices returns the difference between two string slices.
    64  // Useful in tests.
    65  // See:
    66  // http://stackoverflow.com/questions/19374219/how-to-find-the-difference-between-two-slices-of-strings-in-golang
    67  func DiffStringSlices(slice1 []string, slice2 []string) []string {
    68  	diffStr := []string{}
    69  	m := map[string]int{}
    70  
    71  	for _, s1Val := range slice1 {
    72  		m[s1Val] = 1
    73  	}
    74  	for _, s2Val := range slice2 {
    75  		m[s2Val] = m[s2Val] + 1
    76  	}
    77  
    78  	for mKey, mVal := range m {
    79  		if mVal == 1 {
    80  			diffStr = append(diffStr, mKey)
    81  		}
    82  	}
    83  
    84  	return diffStr
    85  }
    86  
    87  // DiffStrings splits the strings into fields and runs it into DiffStringSlices.
    88  // Useful for tests.
    89  func DiffStrings(s1, s2 string) []string {
    90  	return DiffStringSlices(strings.Fields(s1), strings.Fields(s2))
    91  }
    92  
    93  // IsCI reports whether we're running in a CI server.
    94  func IsCI() bool {
    95  	return (os.Getenv("CI") != "" || os.Getenv("CI_LOCAL") != "") && os.Getenv("CIRCLE_BRANCH") == ""
    96  }
    97  
    98  // IsGitHubAction reports whether we're running in a GitHub Action.
    99  func IsGitHubAction() bool {
   100  	return os.Getenv("GITHUB_ACTION") != ""
   101  }
   102  
   103  // SupportsAll reports whether the running system supports all Hugo features,
   104  // e.g. Asciidoc, Pandoc etc.
   105  func SupportsAll() bool {
   106  	return IsGitHubAction()
   107  }
   108  
   109  // GoMinorVersion returns the minor version of the current Go version,
   110  // e.g. 16 for Go 1.16.
   111  func GoMinorVersion() int {
   112  	return extractMinorVersionFromGoTag(runtime.Version())
   113  }
   114  
   115  var goMinorVersionRe = regexp.MustCompile(`go1.(\d*)`)
   116  
   117  func extractMinorVersionFromGoTag(tag string) int {
   118  	// The tag may be on the form go1.17, go1.17.5 go1.17rc2 -- or just a commit hash.
   119  	match := goMinorVersionRe.FindStringSubmatch(tag)
   120  
   121  	if len(match) == 2 {
   122  		i, err := strconv.Atoi(match[1])
   123  		if err != nil {
   124  			return -1
   125  		}
   126  		return i
   127  	}
   128  
   129  	// a commit hash, not useful.
   130  	return -1
   131  
   132  }