e2e/facts_test.go

v1.0.1
gitbay/e2e/facts_test.go history · blame · raw

96 lines · 3694 bytes

 1package e2e
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// TestRepoFacts covers the repository summary: counts, license, languages,
11// and contributors resolved to accounts where the email is verified.
12func TestRepoFacts(t *testing.T) {
13	inst := startInstance(t)
14	aliceKey := inst.newKey(t, "alice")
15	inst.admin(t, "admin", "user", "create", "alice",
16		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
17	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
18		t.Fatalf("repo create: %s", errOut)
19	}
20
21	work := t.TempDir()
22	env := inst.gitEnv(aliceKey)
23	aliceEnv := append(append([]string{}, env...),
24		"GIT_AUTHOR_NAME=Alice", "GIT_AUTHOR_EMAIL=alice@example.test")
25	mustGit(t, work, env, "clone", inst.sshURL("alice/app"), "w")
26	dir := filepath.Join(work, "w")
27
28	os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\n\nfunc main() {}\n"), 0o644)
29	os.WriteFile(filepath.Join(dir, "run.sh"), []byte("#!/bin/sh\necho hi\n"), 0o644)
30	os.WriteFile(filepath.Join(dir, "LICENSE"), []byte(zeroBSD), 0o644)
31	mustGit(t, dir, env, "checkout", "-q", "-b", "main")
32	mustGit(t, dir, env, "add", ".")
33	mustGit(t, dir, aliceEnv, "commit", "-q", "-m", "one")
34	// A second commit by someone with no account here.
35	os.WriteFile(filepath.Join(dir, "extra.go"), []byte("package main\n"), 0o644)
36	mustGit(t, dir, env, "add", ".")
37	mustGit(t, dir, env, "commit", "-q", "-m", "two")
38	mustGit(t, dir, env, "push", "-q", "origin", "main")
39	mustGit(t, dir, env, "tag", "v0.1.0")
40	mustGit(t, dir, env, "push", "-q", "origin", "v0.1.0")
41
42	status, body := inst.get(t, "/alice/app")
43	if status != 200 {
44		t.Fatalf("repo home: %d", status)
45	}
46	for _, want := range []string{
47		"<strong>2</strong> commit", // both commits counted
48		"<strong>1</strong> branch",
49		"<strong>1</strong> tag",
50		"0BSD",  // license detected and surfaced
51		"Go",    // language census
52		"Shell", // and it is not single-language
53		"2 contributors",
54	} {
55		if !strings.Contains(body, want) {
56			t.Errorf("repo home missing %q", want)
57		}
58	}
59	// The contributor with a verified email links to their profile; the
60	// other is named without inventing an account for them.
61	if !strings.Contains(body, `href="/alice"`) {
62		t.Error("verified contributor not linked to their profile")
63	}
64	if strings.Contains(body, `href="/t"`) {
65		t.Error("unknown contributor linked to a profile that does not exist")
66	}
67
68	// Subdirectory listings are about the directory, not the repository.
69	mustGit(t, dir, env, "rm", "-q", "extra.go")
70	os.MkdirAll(filepath.Join(dir, "sub"), 0o755)
71	os.WriteFile(filepath.Join(dir, "sub", "x.go"), []byte("package sub\n"), 0o644)
72	mustGit(t, dir, env, "add", "-A")
73	mustGit(t, dir, env, "commit", "-q", "-m", "sub")
74	mustGit(t, dir, env, "push", "-q", "origin", "main")
75	if _, body := inst.get(t, "/alice/app/tree/main/sub"); strings.Contains(body, "contributor") {
76		t.Error("facts bar rendered on a subdirectory listing")
77	}
78
79	// A repository with no commits has no facts to state, and must still
80	// render — the empty case renders through the same page type.
81	if _, _, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/empty"); code != 0 {
82		t.Fatal("repo create failed")
83	}
84	if status, body := inst.get(t, "/alice/empty"); status != 200 {
85		t.Fatalf("empty repo home: %d", status)
86	} else if strings.Contains(body, "contributor") {
87		t.Error("empty repository claims contributors")
88	}
89}
90
91const zeroBSD = `Permission to use, copy, modify, and/or distribute this software for any
92purpose with or without fee is hereby granted.
93
94THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95WITH REGARD TO THIS SOFTWARE.
96`