src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

test_utils.go (2893B)


      1 // Copyright 2024 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package main
      6 
      7 import (
      8 	"encoding/json"
      9 	"os"
     10 	"path/filepath"
     11 	"regexp"
     12 	"runtime"
     13 	"testing"
     14 )
     15 
     16 // copyTestCase copies the test case at dir into a
     17 // temporary directory. The created files have 0644
     18 // permission and directories 0755. It does not create
     19 // symlinks.
     20 func copyTestCase(dir string, t *testing.T) string {
     21 	newDir, err := filepath.Abs(t.TempDir())
     22 	if err != nil {
     23 		t.Fatalf("failed to copy test case %s: cannot create root %v", dir, err)
     24 	}
     25 
     26 	if err := copyDir(dir, newDir); err != nil {
     27 		t.Fatalf("failed to copy test case %s: copy failure %v", dir, err)
     28 	}
     29 	return newDir
     30 }
     31 
     32 func copyDir(srcDir, destDir string) error {
     33 	entries, err := os.ReadDir(srcDir)
     34 	if err != nil {
     35 		return err
     36 	}
     37 	for _, entry := range entries {
     38 		src := filepath.Join(srcDir, entry.Name())
     39 		dest := filepath.Join(destDir, entry.Name())
     40 
     41 		fileInfo, err := os.Stat(src)
     42 		if err != nil {
     43 			return err
     44 		}
     45 
     46 		switch fileInfo.Mode() & os.ModeType {
     47 		case os.ModeDir:
     48 			if err := os.MkdirAll(dest, 0755); err != nil {
     49 				return err
     50 			}
     51 			if err := copyDir(src, dest); err != nil {
     52 				return err
     53 			}
     54 		default:
     55 			if err := copyFile(src, dest); err != nil {
     56 				return err
     57 			}
     58 		}
     59 	}
     60 	return nil
     61 }
     62 
     63 func copyFile(src, dest string) error {
     64 	b, err := os.ReadFile(src)
     65 	if err != nil {
     66 		return err
     67 	}
     68 	return os.WriteFile(dest, b, 0644)
     69 }
     70 
     71 type config struct {
     72 	// SkipGOOS is a list of GOOS to skip
     73 	SkipGOOS []string `json:"skipGOOS,omitempty"`
     74 	// Copy the folder to isolate it
     75 	Copy bool `json:"copy,omitempty"`
     76 	// SkipBuild the test case
     77 	SkipBuild bool `json:"skipBuild,omitempty"`
     78 	// Strip indicates if binaries should be stripped
     79 	Strip bool `json:"strip,omitempty"`
     80 	// EnableSBOM indicates if sbom should be
     81 	// printed in JSON.
     82 	EnableSBOM bool `json:"sbom,omitempty"`
     83 
     84 	Fixups []fixup `json:"fixups,omitempty"`
     85 }
     86 
     87 func (c *config) skip() bool {
     88 	for _, sg := range c.SkipGOOS {
     89 		if runtime.GOOS == sg {
     90 			return true
     91 		}
     92 	}
     93 	return false
     94 }
     95 
     96 type fixup struct {
     97 	Pattern     string `json:"pattern,omitempty"`
     98 	Replace     string `json:"replace,omitempty"`
     99 	compiled    *regexp.Regexp
    100 	replaceFunc func(b []byte) []byte
    101 }
    102 
    103 func (f *fixup) init() {
    104 	f.compiled = regexp.MustCompile(f.Pattern)
    105 }
    106 
    107 func (f *fixup) apply(data []byte) []byte {
    108 	if f.replaceFunc != nil {
    109 		return f.compiled.ReplaceAllFunc(data, f.replaceFunc)
    110 	}
    111 	return f.compiled.ReplaceAll(data, []byte(f.Replace))
    112 }
    113 
    114 // loadConfig loads and initializes the config from path.
    115 func loadConfig(path string) (*config, error) {
    116 	b, err := os.ReadFile(path)
    117 	if err != nil {
    118 		return nil, err
    119 	}
    120 	var cfg config
    121 	if err := json.Unmarshal(b, &cfg); err != nil {
    122 		return nil, err
    123 	}
    124 	for i := range cfg.Fixups {
    125 		cfg.Fixups[i].init()
    126 	}
    127 	return &cfg, nil
    128 }