Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add extends capability to engine #515

Merged
merged 6 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add more unit tests to increase coverage
  • Loading branch information
marcelosousa committed Dec 9, 2022
commit 5da71f1ac19ed169c45b05f1858edb802f72f434
5 changes: 0 additions & 5 deletions engine/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,7 @@ func processExtends(ctx context.Context, githubClient *gh.GithubClient, file *Re
return nil, fmt.Errorf("loader: cyclic extends dependency")
}

if _, ok := env.Visited[eHash]; ok {
continue
}

env.Stack[eHash] = true
env.Visited[eHash] = true

extensionFile, err := processExtends(ctx, githubClient, eFile, env)
if err != nil {
Expand Down
80 changes: 78 additions & 2 deletions engine/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package engine_test

import (
"context"
"errors"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -122,6 +123,31 @@ func TestLoad(t *testing.T) {
inputReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_inline_rules.yml",
wantReviewpadFilePath: "testdata/loader/process/reviewpad_with_multiple_inline_rules_after_processing.yml",
},
"when the file has non-existing extends": {
inputReviewpadFilePath: "testdata/loader/reviewpad_with_extends.yml",
inputContext: context.Background(),
inputGitHubClient: aladino.MockDefaultGithubClient(
[]mock.MockBackendOption{
mock.WithRequestMatchHandler(
mock.GetReposContentsByOwnerByRepoByPath,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mock.WriteError(
w,
http.StatusInternalServerError,
"loader: extends file not found",
)
}),
),
},
nil,
),
wantErr: "loader: extends file not found",
},
"when the file has invalid extends": {
inputReviewpadFilePath: "testdata/loader/reviewpad_with_invalid_extends.yml",
inputContext: context.Background(),
wantErr: "fatal: url must be a link to a GitHub blob, e.g. https://github.com/reviewpad/action/blob/main/main.go",
},
"when the file has an extends": {
inputReviewpadFilePath: "testdata/loader/reviewpad_with_extends.yml",
wantReviewpadFilePath: "testdata/loader/process/reviewpad_with_extends_after_processing.yml",
Expand Down Expand Up @@ -163,6 +189,47 @@ func TestLoad(t *testing.T) {
nil,
),
},
"when the file has cyclic extends": {
inputReviewpadFilePath: "testdata/loader/reviewpad_with_cyclic_extends_dependency_a.yml",
inputContext: context.Background(),
inputGitHubClient: aladino.MockDefaultGithubClient(
[]mock.MockBackendOption{
mock.WithRequestMatchHandler(
mock.GetReposContentsByOwnerByRepoByPath,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
utils.MustWriteBytes(w, mock.MustMarshal(
[]github.RepositoryContent{
{
Name: github.String("reviewpad_with_cyclic_extends_dependency_a.yml"),
DownloadURL: github.String("https://raw.githubusercontent.com/reviewpad_with_cyclic_extends_dependency_a.yml"),
},
{
Name: github.String("reviewpad_with_cyclic_extends_dependency_b.yml"),
DownloadURL: github.String("https://raw.githubusercontent.com/reviewpad_with_cyclic_extends_dependency_b.yml"),
},
},
))
}),
),
mock.WithRequestMatch(
mock.EndpointPattern{
Pattern: "/reviewpad_with_cyclic_extends_dependency_a.yml",
Method: "GET",
},
httpmock.File("testdata/loader/reviewpad_with_cyclic_extends_dependency_a.yml").Bytes(),
),
mock.WithRequestMatch(
mock.EndpointPattern{
Pattern: "/reviewpad_with_cyclic_extends_dependency_b.yml",
Method: "GET",
},
httpmock.File("testdata/loader/reviewpad_with_cyclic_extends_dependency_b.yml").Bytes(),
),
},
nil,
),
wantErr: "loader: cyclic extends dependency",
},
}

for name, test := range tests {
Expand Down Expand Up @@ -194,8 +261,17 @@ func TestLoad(t *testing.T) {

gotReviewpadFile, gotErr := engine.Load(test.inputContext, test.inputGitHubClient, reviewpadFileData)

if gotErr != nil && gotErr.Error() != test.wantErr {
assert.FailNow(t, "Load() error = %v, wantErr %v", gotErr, test.wantErr)
if gotErr != nil {
githubError := &github.ErrorResponse{}
if errors.As(gotErr, &githubError) {
if githubError.Message != test.wantErr {
assert.FailNow(t, "Load() error = %v, wantErr %v", gotErr, test.wantErr)
}
} else {
if gotErr.Error() != test.wantErr {
assert.FailNow(t, "Load() error = %v, wantErr %v", gotErr, test.wantErr)
}
}
}

assert.Equal(t, wantReviewpadFile, gotReviewpadFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

api-version: reviewpad.com/v3.x

extends:
- https://github.com/reviewpad/reviewpad/blob/main/engine/testdata/reviewpad_with_cyclic_extends_dependency_b.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

api-version: reviewpad.com/v3.x

extends:
- https://github.com/reviewpad/reviewpad/blob/main/engine/testdata/reviewpad_with_cyclic_extends_dependency_a.yml
29 changes: 29 additions & 0 deletions engine/testdata/loader/reviewpad_with_invalid_extends.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2022 Explore.dev Unipessoal Lda. All Rights Reserved.
# Use of this source code is governed by a license that can be
# found in the LICENSE file.

api-version: reviewpad.com/v3.x

extends:
- https://github.com/reviewpad/reviewpad/blo/main/engine/testdata/reviewpad_with_no_extends_a.yml

labels:
small:
color: '#aa12ab'

groups:
- name: owners
kind: developers
spec: '["anonymous"]'

rules:
- name: is-small
kind: patch
spec: $size() < 30

workflows:
- name: add-label-with-small-size
if:
- rule: is-small
then:
- '$addLabel("small")'