Skip to content

Commit 089acf9

Browse files
committed
added tests for ssh archives
1 parent 01220b1 commit 089acf9

File tree

2 files changed

+164
-4
lines changed

2 files changed

+164
-4
lines changed

internal/plugin/git.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,22 @@ func (p *gitPlugin) sshBaseGitCommand() (string, error) {
212212
defaultBranch = "master"
213213
}
214214

215-
p.ref.Ref = defaultBranch
216-
217215
prefix := "git archive --format=tar.gz --remote=ssh://git@"
218216
path, _ := url.JoinPath(p.ref.Owner, p.ref.Repo)
219217
branch := cmp.Or(p.ref.Rev, p.ref.Ref, defaultBranch)
220-
221218
host := p.ref.Host
222219

223220
// the Ref struct defaults the field to 0. This technically a valid port for UDP, but we aren't using UDP
224221
if p.ref.Port > 0 {
225222
host += ":" + fmt.Sprintf("%d", p.ref.Port)
226223
}
227224

228-
command := fmt.Sprintf("%s%s/%s %s %s -o", prefix, host, path, branch, p.ref.Dir)
225+
command := fmt.Sprintf("%s%s/%s %s", prefix, host, path, branch)
226+
if p.ref.Dir != "" {
227+
command += fmt.Sprintf(" %s", p.ref.Dir)
228+
}
229+
command += " -o"
230+
229231
slog.Debug("Generated base git archive command: " + command)
230232
return command, nil
231233
}

internal/plugin/git_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,164 @@ func TestNewGitPlugin(t *testing.T) {
371371
},
372372
expectedURL: "https://api.bitbucket.org/2.0/repositories/username/my-plugin/src/1234567/subdir",
373373
},
374+
{
375+
name: "parse basic ssh plugin",
376+
Include: []flake.Ref{
377+
{
378+
Type: "ssh",
379+
Host: "localhost",
380+
Owner: "username",
381+
Repo: "my-plugin",
382+
},
383+
},
384+
385+
expected: gitPlugin{
386+
ref: flake.Ref{
387+
Type: "ssh",
388+
Host: "localhost",
389+
Owner: "username",
390+
Repo: "my-plugin",
391+
},
392+
name: "username.my-plugin",
393+
},
394+
expectedURL: "git archive --format=tar.gz --remote=ssh://git@localhost/username/my-plugin main -o",
395+
},
396+
{
397+
name: "parse ssh plugin with port",
398+
Include: []flake.Ref{
399+
{
400+
Type: "ssh",
401+
Host: "localhost",
402+
Port: 9999,
403+
Owner: "username",
404+
Repo: "my-plugin",
405+
},
406+
},
407+
408+
expected: gitPlugin{
409+
ref: flake.Ref{
410+
Type: "ssh",
411+
Host: "localhost",
412+
Port: 9999,
413+
Owner: "username",
414+
Repo: "my-plugin",
415+
},
416+
name: "username.my-plugin",
417+
},
418+
expectedURL: "git archive --format=tar.gz --remote=ssh://git@localhost:9999/username/my-plugin main -o",
419+
},
420+
{
421+
name: "parse ssh plugin with port and dir",
422+
Include: []flake.Ref{
423+
{
424+
Type: "ssh",
425+
Host: "localhost",
426+
Port: 9999,
427+
Owner: "username",
428+
Repo: "my-plugin",
429+
Dir: "subdir",
430+
},
431+
},
432+
433+
expected: gitPlugin{
434+
ref: flake.Ref{
435+
Type: "ssh",
436+
Host: "localhost",
437+
Port: 9999,
438+
Owner: "username",
439+
Repo: "my-plugin",
440+
Dir: "subdir",
441+
},
442+
name: "username.my-plugin.subdir",
443+
},
444+
expectedURL: "git archive --format=tar.gz --remote=ssh://git@localhost:9999/username/my-plugin main subdir -o",
445+
},
446+
{
447+
name: "parse ssh plugin with port, dir and rev",
448+
Include: []flake.Ref{
449+
{
450+
Type: "ssh",
451+
Host: "localhost",
452+
Port: 9999,
453+
Owner: "username",
454+
Repo: "my-plugin",
455+
Dir: "subdir",
456+
Rev: "1234567",
457+
},
458+
},
459+
460+
expected: gitPlugin{
461+
ref: flake.Ref{
462+
Type: "ssh",
463+
Host: "localhost",
464+
Port: 9999,
465+
Owner: "username",
466+
Repo: "my-plugin",
467+
Dir: "subdir",
468+
Rev: "1234567",
469+
},
470+
name: "username.my-plugin.subdir",
471+
},
472+
expectedURL: "git archive --format=tar.gz --remote=ssh://git@localhost:9999/username/my-plugin 1234567 subdir -o",
473+
},
474+
{
475+
name: "parse ssh plugin with port, dir and ref",
476+
Include: []flake.Ref{
477+
{
478+
Type: "ssh",
479+
Host: "localhost",
480+
Port: 9999,
481+
Owner: "username",
482+
Repo: "my-plugin",
483+
Dir: "subdir",
484+
Ref: "some/branch",
485+
},
486+
},
487+
488+
expected: gitPlugin{
489+
ref: flake.Ref{
490+
Type: "ssh",
491+
Host: "localhost",
492+
Port: 9999,
493+
Owner: "username",
494+
Repo: "my-plugin",
495+
Dir: "subdir",
496+
Ref: "some/branch",
497+
},
498+
name: "username.my-plugin.subdir",
499+
},
500+
expectedURL: "git archive --format=tar.gz --remote=ssh://git@localhost:9999/username/my-plugin some/branch subdir -o",
501+
},
502+
{
503+
name: "parse ssh plugin with port, dir, ref and ref",
504+
Include: []flake.Ref{
505+
{
506+
Type: "ssh",
507+
Host: "localhost",
508+
Port: 9999,
509+
Owner: "username",
510+
Repo: "my-plugin",
511+
Dir: "subdir",
512+
Ref: "some/branch",
513+
Rev: "1234567",
514+
},
515+
},
516+
517+
expected: gitPlugin{
518+
ref: flake.Ref{
519+
Type: "ssh",
520+
Host: "localhost",
521+
Port: 9999,
522+
Owner: "username",
523+
Repo: "my-plugin",
524+
Dir: "subdir",
525+
Ref: "some/branch",
526+
Rev: "1234567",
527+
},
528+
name: "username.my-plugin.subdir",
529+
},
530+
expectedURL: "git archive --format=tar.gz --remote=ssh://git@localhost:9999/username/my-plugin 1234567 subdir -o",
531+
},
374532
}
375533

376534
for _, testCase := range testCases {

0 commit comments

Comments
 (0)