Skip to content

Commit

Permalink
@ronenlu add test for suggested fixes without text edit (#181)
Browse files Browse the repository at this point in the history
* atlasaction: fix panic in code suggestion

* atlasaction/testdata: create migration dir for dropping col

* add test for suggested fixes without text edit

* add tests
  • Loading branch information
ronenlu committed Jun 26, 2024
1 parent e9135c2 commit c887133
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions atlasaction/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ func (g *githubAPI) addSuggestions(act Action, payload *atlasexec.SummaryReport)
}
for _, report := range file.Reports {
for _, s := range report.SuggestedFixes {
if s.TextEdit == nil {
continue
}
footer := fmt.Sprintf("Ensure to run `atlas migrate hash --dir \"file://%s\"` after applying the suggested changes.", payload.Env.Dir)
body := fmt.Sprintf("%s\n```suggestion\n%s\n```\n%s", s.Message, s.TextEdit.NewText, footer)
if err := g.upsertSuggestion(filePath, body, s); err != nil {
Expand Down
60 changes: 60 additions & 0 deletions atlasaction/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,66 @@ func TestMigrateLint(t *testing.T) {
require.Len(t, comments, 1)
require.Equal(t, "updated comment", comments[0].Body)
})
t.Run("lint summary - no text edit", func(t *testing.T) {
tt := newT(t)
var comments []pullRequestComment
ghMock := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
var (
path = request.URL.Path
method = request.Method
)
switch {
// List comments endpoint
case path == "/repos/test-owner/test-repository/pulls/0/comments" && method == http.MethodGet:
b, err := json.Marshal(comments)
require.NoError(t, err)
_, err = writer.Write(b)
require.NoError(t, err)
return
// Create comment endpoint
case path == "/repos/test-owner/test-repository/pulls/0/comments" && method == http.MethodPost:
var payload pullRequestComment
require.NoError(t, json.NewDecoder(request.Body).Decode(&payload))
payload.ID = 123
comments = append(comments, payload)
writer.WriteHeader(http.StatusCreated)
return
// Update comment endpoint
case path == "/repos/test-owner/test-repository/pulls/comments/123" && method == http.MethodPatch:
require.Len(t, comments, 1)
comments[0].Body = "updated comment"
return
// List pull request files endpoint
case path == "/repos/test-owner/test-repository/pulls/0/files" && method == http.MethodGet:
// language=JSON
_, err := writer.Write([]byte(`[{"filename": "testdata/drop_column/20240626085256_init.sql"}, {"filename": "testdata/drop_column/20240626085324_drop_col.sql"}]`))
require.NoError(t, err)
default:
writer.WriteHeader(http.StatusNotFound)
}
}))
tt.env["GITHUB_API_URL"] = ghMock.URL
tt.env["GITHUB_REPOSITORY"] = "test-owner/test-repository"
tt.setupConfigWithLogin(t, srv.URL, token)
tt.setInput("dev-url", "sqlite://file?mode=memory")
tt.setInput("dir", "file://testdata/drop_column")
tt.setInput("dir-name", "test-dir-slug")
err := MigrateLint(context.Background(), tt.cli, tt.act)
require.ErrorContains(t, err, "https://migration-lint-report-url")
c, err := os.ReadFile(tt.env["GITHUB_STEP_SUMMARY"])
require.NoError(t, err)
sum := string(c)
require.Contains(t, sum, "`atlas migrate lint` on <strong>testdata/drop_column</strong>\n")
require.Contains(t, sum, "2 new migration files detected")
require.Contains(t, sum, "1 reports were found in analysis")
require.Contains(t, sum, `<a href="https://migration-lint-report-url" target="_blank">`)
out := tt.out.String()
require.Contains(t, out, "error file=testdata/drop_column/20240626085324_drop_col.sql")
require.Contains(t, out, "destructive changes detected")
require.Contains(t, out, "Details: https://atlasgo.io/lint/analyzers#DS103")
// There is no suggestion for dropping a column because there are 2 statements in the file
require.Len(t, comments, 0)
})
t.Run("lint summary - lint error - working directory is set", func(t *testing.T) {
tt := newT(t)
// Same as the previous test but with working directory input set.
Expand Down
1 change: 1 addition & 0 deletions atlasaction/testdata/drop_column/20240626085256_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create table t1 ( c int, d int );
2 changes: 2 additions & 0 deletions atlasaction/testdata/drop_column/20240626085324_drop_col.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `t1` DROP COLUMN `c`;
ALTER TABLE `t1` ADD COLUMN `e` int;
3 changes: 3 additions & 0 deletions atlasaction/testdata/drop_column/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1:NrcC8Symi8J7PvPdU8CIf8hzC8XXObXqGyNIBgLPEbs=
20240626085256_init.sql h1:hiU1/tf5NKKW5w+Aw4BtPS97TqVSwuKjq2RjgSj4hTU=
20240626085324_drop_col.sql h1:T/WYcTIo6Hd3zHc0rku8MagdnujdMPTGeexl4btZIGY=

0 comments on commit c887133

Please sign in to comment.