main 98594d0

uses_test.go RawHistoryFolder

1// SPDX-License-Identifier: MIT
2
3package action
4
5import (
6	"encoding/json"
7	"os"
8	"strings"
9	"testing"
10)
11
12// TestParseUsesAgainstRunner replays testdata/uses.json: for each line of
13// testdata/uses.txt, what the runner's own converter makes of it as the
14// "uses" of a composite step (gen/runner).
15func TestParseUsesAgainstRunner(t *testing.T) {
16	data, err := os.ReadFile("testdata/uses.json")
17	if err != nil {
18		t.Fatal(err)
19	}
20	var want map[string]runnerAnswer
21	if err := json.Unmarshal(data, &want); err != nil {
22		t.Fatal(err)
23	}
24	lines, _ := os.ReadFile("testdata/uses.txt")
25	n := 0
26	for _, line := range strings.Split(strings.TrimSuffix(string(lines), "\n"), "\n") {
27		w, ok := want[line]
28		if !ok {
29			t.Errorf("%q: not in uses.json", line)
30			continue
31		}
32		n++
33		u, err := ParseUses(line)
34		if len(w.Errors) > 0 {
35			if err == nil {
36				t.Errorf("%q: accepted as %+v, the runner says %q", line, u, w.Errors[0])
37				continue
38			}
39			// "u.yml (Line: 6, Col: 13): <message>"
40			_, msg, _ := strings.Cut(w.Errors[0], "): ")
41			if err.Error() != msg {
42				t.Errorf("%q: %q, the runner says %q", line, err.Error(), msg)
43			}
44			continue
45		}
46		if err != nil {
47			t.Errorf("%q: %v, the runner accepts it", line, err)
48			continue
49		}
50		ref := w.Execution.Steps[0].Reference
51		checkReference(t, 0, u, ref)
52	}
53	if n < 60 {
54		t.Errorf("only %d values compared", n)
55	}
56}
57
58func TestParseUses(t *testing.T) {
59	tests := []struct {
60		in   string
61		want Uses
62		err  string
63	}{
64		{in: "actions/checkout@v4", want: Uses{Kind: UsesRepository, Owner: "actions", Repo: "checkout", Name: "actions/checkout", Ref: "v4"}},
65		{in: "actions/aws/ec2@main", want: Uses{Kind: UsesRepository, Owner: "actions", Repo: "aws", Name: "actions/aws", Path: "ec2", Ref: "main"}},
66		{in: `a\b\c@v1`, want: Uses{Kind: UsesRepository, Owner: "a", Repo: "b", Name: "a/b", Path: "c", Ref: "v1"}},
67		{in: "/a//b/@c", want: Uses{Kind: UsesRepository, Owner: "a", Repo: "b", Name: "a/b", Ref: "c"}},
68		{in: "owner/repo/.github/workflows/ci.yml@main", want: Uses{Kind: UsesRepository, Owner: "owner", Repo: "repo", Name: "owner/repo", Path: ".github/workflows/ci.yml", Ref: "main"}},
69		{in: "./local/path", want: Uses{Kind: UsesLocal, Path: "./local/path"}},
70		{in: `.\local\path`, want: Uses{Kind: UsesLocal, Path: `.\local\path`}},
71		{in: "docker://alpine:3.8", want: Uses{Kind: UsesDocker, Image: "alpine:3.8"}},
72		{in: "docker://gcr.io/my-project/my-image:latest", want: Uses{Kind: UsesDocker, Image: "gcr.io/my-project/my-image:latest"}},
73		{in: "$/", want: Uses{Kind: UsesSelfRepository}},
74		{in: "$//a/b", want: Uses{Kind: UsesSelfRepository, Path: "a/b"}},
75		{in: "", err: "Expected format {org}/{repo}[/path]@ref. Actual ''"},
76		{in: "actions/checkout", err: "Expected format {org}/{repo}[/path]@ref. Actual 'actions/checkout'"},
77		{in: "actions/checkout@", err: "Expected format {org}/{repo}[/path]@ref. Actual 'actions/checkout@'"},
78		{in: "checkout@v4", err: "Expected format {org}/{repo}[/path]@ref. Actual 'checkout@v4'"},
79		{in: "/repo@v4", err: "Expected format {org}/{repo}[/path]@ref. Actual '/repo@v4'"},
80		{in: "owner/@v4", err: "Expected format {org}/{repo}[/path]@ref. Actual 'owner/@v4'"},
81		{in: "a/b@c@d", err: "Expected format {org}/{repo}[/path]@ref. Actual 'a/b@c@d'"},
82		{in: "Docker://alpine", err: "Expected format {org}/{repo}[/path]@ref. Actual 'Docker://alpine'"},
83		{in: "$/foo@v1", err: "Self-repository references do not support an '@ref' suffix. Actual '$/foo@v1'"},
84	}
85	for _, tc := range tests {
86		got, err := ParseUses(tc.in)
87		if tc.err != "" {
88			if err == nil || err.Error() != tc.err {
89				t.Errorf("ParseUses(%q): err %v, want %q", tc.in, err, tc.err)
90			}
91			var ue *UsesError
92			if err != nil && (!asUsesError(err, &ue) || ue.Value != tc.in) {
93				t.Errorf("ParseUses(%q): error %#v does not carry the value", tc.in, err)
94			}
95			continue
96		}
97		if err != nil || got != tc.want {
98			t.Errorf("ParseUses(%q) = %+v, %v; want %+v", tc.in, got, err, tc.want)
99		}
100	}
101}
102
103func asUsesError(err error, target **UsesError) bool {
104	ue, ok := err.(*UsesError)
105	*target = ue
106	return ok
107}
108
109func TestParseImage(t *testing.T) {
110	tests := []struct {
111		in   string
112		want Image
113		err  string
114	}{
115		{in: "Dockerfile", want: Image{Kind: ImageDockerfile, Path: "Dockerfile"}},
116		{in: "images/Dockerfile", want: Image{Kind: ImageDockerfile, Path: "images/Dockerfile"}},
117		{in: "dockerfile", want: Image{Kind: ImageDockerfile, Path: "dockerfile"}},
118		{in: "a/Dockerfile.prod", want: Image{Kind: ImageDockerfile, Path: "a/Dockerfile.prod"}},
119		{in: "a/prod.Dockerfile", want: Image{Kind: ImageDockerfile, Path: "a/prod.Dockerfile"}},
120		{in: "docker://ubuntu:18.04", want: Image{Kind: ImageRegistry, Ref: "ubuntu:18.04"}},
121		{in: "Docker://alpine", want: Image{Kind: ImageRegistry, Ref: "alpine"}},
122		{in: "docker://my/Dockerfile", want: Image{Kind: ImageRegistry, Ref: "my/Dockerfile"}},
123		{in: "alpine:3", err: "'alpine:3' should be either '[path]/Dockerfile' or 'docker://image[:tag]'."},
124		{in: "Dockerfile/readme", err: "'Dockerfile/readme' should be either '[path]/Dockerfile' or 'docker://image[:tag]'."},
125		{in: "", err: "'' should be either '[path]/Dockerfile' or 'docker://image[:tag]'."},
126	}
127	for _, tc := range tests {
128		got, err := ParseImage(tc.in)
129		if tc.err != "" {
130			if err == nil || err.Error() != tc.err {
131				t.Errorf("ParseImage(%q): err %v, want %q", tc.in, err, tc.err)
132			}
133			continue
134		}
135		if err != nil || got != tc.want {
136			t.Errorf("ParseImage(%q) = %+v, %v; want %+v", tc.in, got, err, tc.want)
137		}
138	}
139}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 3

↑↓ moveenter openesc close