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
15package main
16
17import (
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
28func 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 env := vcs.TestEnv()
42 mustRunCmd(t, dir, env, "git", "init")
43 mustRunCmd(t, dir, env, "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
74func mustRunCmd(t *testing.T, dir string, env []string, exe string, args ...string) {
75 cmd := exec.Command(exe, args...)
76 cmd.Dir = dir
77 cmd.Env = env
78 data, err := cmd.CombinedOutput()
79 qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data))
80}