(t *testing.T)
| 48 | } |
| 49 | |
| 50 | func TestCmdRepo(t *testing.T) { |
| 51 | tests := []struct { |
| 52 | name string |
| 53 | args []string |
| 54 | duplicate bool |
| 55 | host string |
| 56 | branch string |
| 57 | err string |
| 58 | relative bool |
| 59 | repo string |
| 60 | }{ |
| 61 | {name: "empty remote", args: []string{"octo/hello"}, host: dotcomHost, branch: "main"}, |
| 62 | {name: "synthetic namespace", args: []string{"example.invalid/hello"}, host: dotcomHost, branch: "main", repo: "example.invalid/hello"}, |
| 63 | {name: "relative state directory", args: []string{"octo/hello"}, host: dotcomHost, branch: "main", relative: true}, |
| 64 | {name: "custom host and branch", args: []string{"example.com/octo/hello", "--branch", "topic/one"}, host: "example.com", branch: "topic/one"}, |
| 65 | {name: "never overwrites", args: []string{"octo/hello"}, duplicate: true, err: "already exists"}, |
| 66 | {name: "invalid Git branch", args: []string{"octo/hello", "--branch", "bad..ref"}, err: `invalid branch "bad..ref"`}, |
| 67 | } |
| 68 | for _, tt := range tests { |
| 69 | t.Run(tt.name, func(t *testing.T) { |
| 70 | dir := t.TempDir() |
| 71 | if tt.relative { |
| 72 | t.Chdir(dir) |
| 73 | dir = "state" |
| 74 | } |
| 75 | t.Setenv("GHMOCK_DIR", dir) |
| 76 | if tt.duplicate { |
| 77 | require.NoError(t, cmdRepo(tt.args, io.Discard)) |
| 78 | } |
| 79 | var out bytes.Buffer |
| 80 | err := cmdRepo(tt.args, &out) |
| 81 | if tt.err != "" { |
| 82 | require.ErrorContains(t, err, tt.err) |
| 83 | return |
| 84 | } |
| 85 | require.NoError(t, err) |
| 86 | repo := tt.repo |
| 87 | if repo == "" { |
| 88 | repo = "octo/hello" |
| 89 | } |
| 90 | assert.Equal(t, fmt.Sprintf("Created %s/%s (branch %s)\nHTTPS: https://%s/%s.git\nSSH: git@%s:%s.git\n", tt.host, repo, tt.branch, tt.host, repo, tt.host, repo), out.String()) |
| 91 | st, err := openState() |
| 92 | require.NoError(t, err) |
| 93 | head, err := os.ReadFile(filepath.Join(gitRepoPath(st.gitFixtureDir(), tt.host, repo), "HEAD")) |
| 94 | require.NoError(t, err) |
| 95 | assert.Equal(t, "ref: refs/heads/"+tt.branch+"\n", string(head)) |
| 96 | }) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestParseWriteArgs(t *testing.T) { |
| 101 | tests := []struct { |
nothing calls this directly
no test coverage detected