cuelang.org/go@v0.10.1/internal/ci/checks/commit_test.go (about) 1 // Copyright 2024 CUE Authors 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "io/fs" 19 "os/exec" 20 "testing" 21 22 "github.com/go-quicktest/qt" 23 "golang.org/x/tools/txtar" 24 25 "cuelang.org/go/internal/vcs" 26 ) 27 28 func TestCommits(t *testing.T) { 29 archive, err := txtar.ParseFile("testdata/checks.txtar") 30 qt.Assert(t, qt.IsNil(err)) 31 archiveFS, err := txtar.FS(archive) 32 qt.Assert(t, qt.IsNil(err)) 33 34 setupCommit := func(t *testing.T, name string) string { 35 commit, err := fs.ReadFile(archiveFS, name) 36 qt.Assert(t, qt.IsNil(err)) 37 38 t.Logf("commit:\n%s", commit) 39 40 dir := t.TempDir() 41 vcs.InitTestEnv(t) 42 mustRunCmd(t, dir, "git", "init") 43 mustRunCmd(t, dir, "git", 44 "-c", "user.email=cueckoo@gmail.com", 45 "-c", "user.name=cueckoo", 46 "commit", "--allow-empty", "-m", string(commit), 47 ) 48 return dir 49 } 50 51 passFiles, err := fs.Glob(archiveFS, "pass-*") 52 qt.Assert(t, qt.IsNil(err)) 53 for _, name := range passFiles { 54 t.Run(name, func(t *testing.T) { 55 dir := setupCommit(t, name) 56 err = checkCommit(dir) 57 t.Logf("error: %v", err) 58 qt.Assert(t, qt.IsNil(err)) 59 }) 60 } 61 62 failFiles, err := fs.Glob(archiveFS, "fail-*") 63 qt.Assert(t, qt.IsNil(err)) 64 for _, name := range failFiles { 65 t.Run(name, func(t *testing.T) { 66 dir := setupCommit(t, name) 67 err = checkCommit(dir) 68 t.Logf("error: %v", err) 69 qt.Assert(t, qt.IsNotNil(err)) 70 }) 71 } 72 } 73 74 func mustRunCmd(t *testing.T, dir string, exe string, args ...string) { 75 cmd := exec.Command(exe, args...) 76 cmd.Dir = dir 77 data, err := cmd.CombinedOutput() 78 qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data)) 79 }