Skip to content

Commit a2ec386

Browse files
author
Kristian Dalmo Olsen
authored
Add e2e test for submodules (#199)
* Ocular patdown of e2e tests * Remove global flag from git config * Fix e2e tests for comments * Add e2e tests for submodules
1 parent c646b78 commit a2ec386

2 files changed

Lines changed: 90 additions & 17 deletions

File tree

e2e/e2e_test.go

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ var (
3535
)
3636

3737
func TestCheckE2E(t *testing.T) {
38-
3938
tests := []struct {
4039
description string
4140
source resource.Source
@@ -472,22 +471,98 @@ func TestGetAndPutE2E(t *testing.T) {
472471
}
473472
}
474473

474+
func TestGetSubmodules(t *testing.T) {
475+
tests := []struct {
476+
description string
477+
source resource.Source
478+
version resource.Version
479+
getParameters resource.GetParameters
480+
expectedFiles []string
481+
}{
482+
{
483+
description: "get works with submodules",
484+
source: resource.Source{
485+
Repository: "itsdalmo/test-repository-active",
486+
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
487+
},
488+
version: resource.Version{
489+
PR: "4",
490+
Commit: "49398613d1f23d14518aadf6023cddba5db649ee",
491+
},
492+
getParameters: resource.GetParameters{
493+
Submodules: true,
494+
},
495+
expectedFiles: []string{
496+
".git",
497+
"README.md",
498+
"latest-test.txt",
499+
"new-test.txt",
500+
"pipeline.yml",
501+
"test.txt",
502+
},
503+
},
504+
{
505+
description: "submodules are optional",
506+
source: resource.Source{
507+
Repository: "itsdalmo/test-repository-active",
508+
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
509+
},
510+
version: resource.Version{
511+
PR: "4",
512+
Commit: "49398613d1f23d14518aadf6023cddba5db649ee",
513+
},
514+
getParameters: resource.GetParameters{
515+
Submodules: false,
516+
},
517+
expectedFiles: []string{},
518+
},
519+
}
520+
521+
for _, tc := range tests {
522+
t.Run(tc.description, func(t *testing.T) {
523+
// Create temporary directory
524+
dir, err := ioutil.TempDir("", "github-pr-resource")
525+
require.NoError(t, err)
526+
defer os.RemoveAll(dir)
527+
528+
githubClient, err := resource.NewGithubClient(&tc.source)
529+
require.NoError(t, err)
530+
531+
git, err := resource.NewGitClient(&tc.source, dir, ioutil.Discard)
532+
require.NoError(t, err)
533+
534+
// Get (output and files)
535+
getRequest := resource.GetRequest{Source: tc.source, Version: tc.version, Params: tc.getParameters}
536+
_, err = resource.Get(getRequest, githubClient, git, dir)
537+
require.NoError(t, err)
538+
539+
files, err := ioutil.ReadDir(filepath.Join(dir, "submodule"))
540+
require.NoError(t, err)
541+
542+
for _, f := range files {
543+
assert.Contains(t, tc.expectedFiles, f.Name())
544+
}
545+
})
546+
}
547+
}
548+
475549
func TestPutCommentsE2E(t *testing.T) {
476-
owner := "itsdalmo"
477-
repo := "github-pr-resource-e2e"
550+
var (
551+
owner = "itsdalmo"
552+
repository = "test-repository-active"
553+
)
478554

479555
tests := []struct {
480-
description, branch string
556+
description string
481557
source resource.Source
482558
getParams resource.GetParameters
483559
putParameters resource.PutParameters
484560
previousComments, expectedComments []string
485561
}{
486562
{
487563
description: "delete previous comments removes old comments and makes new one",
488-
branch: "delete-previous-comments-remove-old-add-new",
489564
source: resource.Source{
490-
Repository: fmt.Sprintf("%s/%s", owner, repo),
565+
Repository: fmt.Sprintf("%s/%s", owner, repository),
491566
V3Endpoint: "https://api.github.com/",
492567
V4Endpoint: "https://api.github.com/graphql",
493568
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
@@ -504,9 +579,8 @@ func TestPutCommentsE2E(t *testing.T) {
504579
},
505580
{
506581
description: "delete previous comments removes all comments when no new comment",
507-
branch: "delete-previous-comments-remove-old",
508582
source: resource.Source{
509-
Repository: fmt.Sprintf("%s/%s", owner, repo),
583+
Repository: fmt.Sprintf("%s/%s", owner, repository),
510584
V3Endpoint: "https://api.github.com/",
511585
V4Endpoint: "https://api.github.com/graphql",
512586
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
@@ -520,9 +594,8 @@ func TestPutCommentsE2E(t *testing.T) {
520594
},
521595
{
522596
description: "delete previous comments should not delete comments when false",
523-
branch: "delete-previous-comments-false",
524597
source: resource.Source{
525-
Repository: fmt.Sprintf("%s/%s", owner, repo),
598+
Repository: fmt.Sprintf("%s/%s", owner, repository),
526599
V3Endpoint: "https://api.github.com/",
527600
V4Endpoint: "https://api.github.com/graphql",
528601
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
@@ -552,15 +625,15 @@ func TestPutCommentsE2E(t *testing.T) {
552625
git, err := resource.NewGitClient(&tc.source, dir, ioutil.Discard)
553626
require.NoError(t, err)
554627

555-
pullRequest, _, err := githubClient.V3.PullRequests.Create(context.TODO(), owner, repo, &github.NewPullRequest{
628+
pullRequest, _, err := githubClient.V3.PullRequests.Create(context.TODO(), owner, repository, &github.NewPullRequest{
556629
Title: github.String(tc.description),
557630
Base: github.String("master"),
558-
Head: github.String(fmt.Sprintf("%s:%s", owner, tc.branch)),
631+
Head: github.String(fmt.Sprintf("%s:%s", owner, "test-comments")),
559632
})
560633
require.NoError(t, err)
561634

562635
for _, comment := range tc.previousComments {
563-
_, _, err = githubClient.V3.Issues.CreateComment(context.TODO(), owner, repo, pullRequest.GetNumber(), &github.IssueComment{
636+
_, _, err = githubClient.V3.Issues.CreateComment(context.TODO(), owner, repository, pullRequest.GetNumber(), &github.IssueComment{
564637
Body: github.String(comment),
565638
})
566639
require.NoError(t, err)
@@ -581,15 +654,15 @@ func TestPutCommentsE2E(t *testing.T) {
581654
_, err = resource.Put(putRequest, githubClient, dir)
582655
require.NoError(t, err)
583656

584-
comments, _, err := githubClient.V3.Issues.ListComments(context.TODO(), owner, repo, pullRequest.GetNumber(), nil)
657+
comments, _, err := githubClient.V3.Issues.ListComments(context.TODO(), owner, repository, pullRequest.GetNumber(), nil)
585658
require.NoError(t, err)
586659

587660
require.Len(t, comments, len(tc.expectedComments))
588661
for index, comment := range comments {
589662
require.Equal(t, tc.expectedComments[index], comment.GetBody())
590663
}
591664

592-
_, _, err = githubClient.V3.PullRequests.Edit(context.TODO(), owner, repo, pullRequest.GetNumber(), &github.PullRequest{
665+
_, _, err = githubClient.V3.PullRequests.Edit(context.TODO(), owner, repository, pullRequest.GetNumber(), &github.PullRequest{
593666
State: github.String("closed"),
594667
})
595668
require.NoError(t, err)

git.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ func (g *GitClient) Init(branch string) error {
7171
if err := g.command("git", "config", "user.email", "concourse@local").Run(); err != nil {
7272
return fmt.Errorf("failed to configure git email: %s", err)
7373
}
74-
if err := g.command("git", "config", "--global", "url.https://x-oauth-basic@github.com/.insteadOf", "git@github.com:").Run(); err != nil {
74+
if err := g.command("git", "config", "url.https://x-oauth-basic@github.com/.insteadOf", "git@github.com:").Run(); err != nil {
7575
return fmt.Errorf("failed to configure github url: %s", err)
7676
}
77-
if err := g.command("git", "config", "--global", "url.https://.insteadOf", "git://").Run(); err != nil {
77+
if err := g.command("git", "config", "url.https://.insteadOf", "git://").Run(); err != nil {
7878
return fmt.Errorf("failed to configure github url: %s", err)
7979
}
8080
return nil

0 commit comments

Comments
 (0)