github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/corpus_test.go (about) 1 package main 2 3 import ( 4 "flag" 5 "os" 6 "os/exec" 7 "strings" 8 "sync" 9 "testing" 10 11 "golang.org/x/tools/go/buildutil" 12 yaml "gopkg.in/yaml.v2" 13 ) 14 15 /* 16 This contains code from https://github.com/dgryski/tinygo-test-corpus 17 18 The MIT License (MIT) 19 20 Copyright (c) 2020 Damian Gryski <damian@gryski.com> 21 22 Permission is hereby granted, free of charge, to any person obtaining a copy 23 of this software and associated documentation files (the "Software"), to deal 24 in the Software without restriction, including without limitation the rights 25 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 26 copies of the Software, and to permit persons to whom the Software is 27 furnished to do so, subject to the following conditions: 28 29 The above copyright notice and this permission notice shall be included in 30 all copies or substantial portions of the Software. 31 32 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 33 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 34 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 35 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 36 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 37 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 38 THE SOFTWARE. 39 40 */ 41 42 var corpus = flag.String("corpus", "", "path to test corpus") 43 44 func TestCorpus(t *testing.T) { 45 t.Parallel() 46 if *corpus == "" { 47 t.Skip() 48 } 49 50 var target string 51 if *testTarget != "" { 52 target = *testTarget 53 } 54 isWASI := strings.HasPrefix(target, "wasi") 55 56 repos, err := loadRepos(*corpus) 57 if err != nil { 58 t.Fatalf("loading corpus: %v", err) 59 } 60 61 for _, repo := range repos { 62 repo := repo 63 name := repo.Repo 64 if repo.Tags != "" { 65 name += "(" + strings.ReplaceAll(repo.Tags, " ", "-") + ")" 66 } 67 if repo.Version != "" { 68 name += "@" + repo.Version 69 } 70 t.Run(name, func(t *testing.T) { 71 t.Parallel() 72 73 if isWASI && repo.SkipWASI { 74 t.Skip("skip wasi") 75 } 76 if repo.Slow && testing.Short() { 77 t.Skip("slow test") 78 } 79 80 var wg sync.WaitGroup 81 defer wg.Wait() 82 out := ioLogger(t, &wg) 83 defer out.Close() 84 85 dir := t.TempDir() 86 cmd := exec.Command("go", "mod", "init", "github.com/tinygo/tinygo-corpus-test") 87 cmd.Dir = dir 88 cmd.Stdout, cmd.Stderr = out, out 89 err := cmd.Run() 90 if err != nil { 91 t.Errorf("failed to init: %s", err.Error()) 92 return 93 } 94 95 var ver string 96 if repo.Version != "" { 97 ver = "@" + repo.Version 98 } 99 cmd = exec.Command("go", "get", "-t", "-d", repo.Repo+"/..."+ver) 100 cmd.Dir = dir 101 cmd.Stdout, cmd.Stderr = out, out 102 err = cmd.Run() 103 if err != nil { 104 t.Errorf("failed to get: %s", err.Error()) 105 return 106 } 107 108 doTest := func(t *testing.T, path string) { 109 var wg sync.WaitGroup 110 defer wg.Wait() 111 out := ioLogger(t, &wg) 112 defer out.Close() 113 114 opts := optionsFromTarget(target, sema) 115 opts.Directory = dir 116 var tags buildutil.TagsFlag 117 tags.Set(repo.Tags) 118 opts.Tags = []string(tags) 119 opts.TestConfig.Verbose = testing.Verbose() 120 121 passed, err := Test(path, out, out, &opts, "") 122 if err != nil { 123 t.Errorf("test error: %v", err) 124 } 125 if !passed { 126 t.Error("test failed") 127 } 128 } 129 if len(repo.Subdirs) == 0 { 130 doTest(t, repo.Repo) 131 return 132 } 133 134 for _, dir := range repo.Subdirs { 135 dir := dir 136 t.Run(dir.Pkg, func(t *testing.T) { 137 t.Parallel() 138 139 if isWASI && dir.SkipWASI { 140 t.Skip("skip wasi") 141 } 142 if dir.Slow && testing.Short() { 143 t.Skip("slow test") 144 } 145 146 doTest(t, repo.Repo+"/"+dir.Pkg) 147 }) 148 } 149 }) 150 } 151 } 152 153 type T struct { 154 Repo string 155 Tags string 156 Subdirs []Subdir 157 SkipWASI bool 158 Slow bool 159 Version string 160 } 161 162 type Subdir struct { 163 Pkg string 164 SkipWASI bool 165 Slow bool 166 } 167 168 func loadRepos(f string) ([]T, error) { 169 170 yf, err := os.ReadFile(f) 171 if err != nil { 172 return nil, err 173 } 174 175 var repos []T 176 err = yaml.Unmarshal(yf, &repos) 177 if err != nil { 178 return nil, err 179 } 180 181 return repos, nil 182 }