-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Reuven Harrison
authored
Dec 10, 2023
1 parent
2410920
commit 04c0f8c
Showing
2 changed files
with
190 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,185 +1,184 @@ | ||
package formatters_test | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tufin/oasdiff/checker" | ||
"github.com/tufin/oasdiff/formatters" | ||
) | ||
|
||
var gitHubFormatter = formatters.GitHubActionsFormatter{ | ||
Localizer: MockLocalizer, | ||
} | ||
|
||
func TestGithubActionsLookup(t *testing.T) { | ||
f, err := formatters.Lookup(string(formatters.FormatGithubActions), formatters.DefaultFormatterOpts()) | ||
require.NoError(t, err) | ||
require.IsType(t, formatters.GitHubActionsFormatter{}, f) | ||
} | ||
|
||
func TestGitHubActionsFormatter_RenderBreakingChanges_OneFailure(t *testing.T) { | ||
testChanges := checker.Changes{ | ||
checker.ApiChange{ | ||
Id: "change_id", | ||
Level: checker.ERR, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
} | ||
|
||
// check output | ||
output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
assert.NoError(t, err) | ||
expectedOutput := "::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n" | ||
assert.Equal(t, expectedOutput, string(output)) | ||
} | ||
|
||
func TestGitHubActionsFormatter_RenderBreakingChanges_MultipleLevels(t *testing.T) { | ||
testChanges := checker.Changes{ | ||
checker.ApiChange{ | ||
Id: "change_id", | ||
Level: checker.ERR, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
checker.ApiChange{ | ||
Id: "warning_id", | ||
Level: checker.WARN, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
checker.ApiChange{ | ||
Id: "notice_id", | ||
Level: checker.INFO, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
} | ||
|
||
// check output | ||
output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
assert.NoError(t, err) | ||
expectedOutput := "::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n::warning title=warning_id::at openapi.yaml, in API GET /api/test This is a warning.\n::notice title=notice_id::at openapi.yaml, in API GET /api/test This is a notice.\n" | ||
assert.Equal(t, expectedOutput, string(output)) | ||
} | ||
|
||
func TestGitHubActionsFormatter_RenderBreakingChanges_MultilineText(t *testing.T) { | ||
t.Skip("messages should not contain \n so this case should never happen") | ||
testChanges := checker.Changes{ | ||
checker.ApiChange{ | ||
Id: "change_two_lines_id", | ||
Level: checker.ERR, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
} | ||
|
||
// check output | ||
output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
assert.NoError(t, err) | ||
expectedOutput := "::error title=change_two_lines_id::at openapi.yaml, in API GET /api/test This is a breaking change.%0AThis is a second line.\n" | ||
assert.Equal(t, expectedOutput, string(output)) | ||
} | ||
|
||
func TestGitHubActionsFormatter_RenderBreakingChanges_FileLocation(t *testing.T) { | ||
testChanges := checker.Changes{ | ||
checker.ApiChange{ | ||
Id: "change_id", | ||
Level: checker.ERR, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
SourceFile: "openapi.json", | ||
SourceLine: 20, | ||
SourceLineEnd: 25, | ||
SourceColumn: 5, | ||
SourceColumnEnd: 10, | ||
}, | ||
} | ||
|
||
// check output | ||
output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
assert.NoError(t, err) | ||
expectedOutput := "::error title=change_id,file=openapi.json,col=6,endColumn=11,line=21,endLine=26::at openapi.yaml, in API GET /api/test This is a breaking change.\n" | ||
assert.Equal(t, expectedOutput, string(output)) | ||
} | ||
|
||
func TestGitHubActionsFormatter_RenderBreakingChanges_JobOutputParameters(t *testing.T) { | ||
// temp file to mock GITHUB_OUTPUT | ||
tempFile, err := os.CreateTemp("", "github-output") | ||
assert.NoError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
_ = os.Setenv("GITHUB_OUTPUT", tempFile.Name()) | ||
|
||
testChanges := checker.Changes{ | ||
checker.ApiChange{ | ||
Id: "change_id", | ||
Level: checker.ERR, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
checker.ApiChange{ | ||
Id: "change_id", | ||
Level: checker.ERR, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
checker.ApiChange{ | ||
Id: "warning_id", | ||
Level: checker.WARN, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
checker.ApiChange{ | ||
Id: "notice_id", | ||
Level: checker.INFO, | ||
Operation: http.MethodGet, | ||
Path: "/api/test", | ||
Source: "openapi.yaml", | ||
}, | ||
} | ||
|
||
// check output | ||
output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
assert.NoError(t, err) | ||
_ = os.Unsetenv("GITHUB_OUTPUT") | ||
expectedOutput := "::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n::warning title=warning_id::at openapi.yaml, in API GET /api/test This is a warning.\n::notice title=notice_id::at openapi.yaml, in API GET /api/test This is a notice.\n" | ||
assert.Equal(t, expectedOutput, string(output)) | ||
|
||
// check job output parameters (NOTE: order of parameters is not guaranteed) | ||
outputFile, err := os.ReadFile(tempFile.Name()) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(outputFile), "error_count=2\n") | ||
assert.Contains(t, string(outputFile), "warning_count=1\n") | ||
assert.Contains(t, string(outputFile), "info_count=1\n") | ||
} | ||
|
||
func TestGitHubActionsFormatter_NotImplemented(t *testing.T) { | ||
var err error | ||
_, err = gitHubFormatter.RenderDiff(nil, formatters.NewRenderOpts()) | ||
assert.Error(t, err) | ||
|
||
_, err = gitHubFormatter.RenderSummary(nil, formatters.NewRenderOpts()) | ||
assert.Error(t, err) | ||
|
||
_, err = gitHubFormatter.RenderChangelog(nil, formatters.NewRenderOpts(), nil) | ||
assert.Error(t, err) | ||
|
||
_, err = gitHubFormatter.RenderChecks(nil, formatters.NewRenderOpts()) | ||
assert.Error(t, err) | ||
|
||
_, err = gitHubFormatter.RenderFlatten(nil, formatters.NewRenderOpts()) | ||
assert.Error(t, err) | ||
} | ||
// import ( | ||
// "net/http" | ||
// "os" | ||
// "testing" | ||
|
||
// "github.com/stretchr/testify/assert" | ||
// "github.com/stretchr/testify/require" | ||
// "github.com/tufin/oasdiff/checker" | ||
// "github.com/tufin/oasdiff/formatters" | ||
// ) | ||
|
||
// var gitHubFormatter = formatters.GitHubActionsFormatter{ | ||
// Localizer: MockLocalizer, | ||
// } | ||
|
||
// func TestGithubActionsLookup(t *testing.T) { | ||
// f, err := formatters.Lookup(string(formatters.FormatGithubActions), formatters.DefaultFormatterOpts()) | ||
// require.NoError(t, err) | ||
// require.IsType(t, formatters.GitHubActionsFormatter{}, f) | ||
// } | ||
|
||
// func TestGitHubActionsFormatter_RenderBreakingChanges_OneFailure(t *testing.T) { | ||
// testChanges := checker.Changes{ | ||
// checker.ApiChange{ | ||
// Id: "change_id", | ||
// Level: checker.ERR, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// } | ||
|
||
// // check output | ||
// output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
// assert.NoError(t, err) | ||
// expectedOutput := "::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n" | ||
// assert.Equal(t, expectedOutput, string(output)) | ||
// } | ||
|
||
// func TestGitHubActionsFormatter_RenderBreakingChanges_MultipleLevels(t *testing.T) { | ||
// testChanges := checker.Changes{ | ||
// checker.ApiChange{ | ||
// Id: "change_id", | ||
// Level: checker.ERR, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// checker.ApiChange{ | ||
// Id: "warning_id", | ||
// Level: checker.WARN, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// checker.ApiChange{ | ||
// Id: "notice_id", | ||
// Level: checker.INFO, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// } | ||
|
||
// // check output | ||
// output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
// assert.NoError(t, err) | ||
// expectedOutput := "::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n::warning title=warning_id::at openapi.yaml, in API GET /api/test This is a warning.\n::notice title=notice_id::at openapi.yaml, in API GET /api/test This is a notice.\n" | ||
// assert.Equal(t, expectedOutput, string(output)) | ||
// } | ||
|
||
// func TestGitHubActionsFormatter_RenderBreakingChanges_MultilineText(t *testing.T) { | ||
// testChanges := checker.Changes{ | ||
// checker.ApiChange{ | ||
// Id: "change_two_lines_id", | ||
// Level: checker.ERR, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// } | ||
|
||
// // check output | ||
// output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
// assert.NoError(t, err) | ||
// expectedOutput := "::error title=change_two_lines_id::at openapi.yaml, in API GET /api/test This is a breaking change.%0AThis is a second line.\n" | ||
// assert.Equal(t, expectedOutput, string(output)) | ||
// } | ||
|
||
// func TestGitHubActionsFormatter_RenderBreakingChanges_FileLocation(t *testing.T) { | ||
// testChanges := checker.Changes{ | ||
// checker.ApiChange{ | ||
// Id: "change_id", | ||
// Level: checker.ERR, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// SourceFile: "openapi.json", | ||
// SourceLine: 20, | ||
// SourceLineEnd: 25, | ||
// SourceColumn: 5, | ||
// SourceColumnEnd: 10, | ||
// }, | ||
// } | ||
|
||
// // check output | ||
// output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
// assert.NoError(t, err) | ||
// expectedOutput := "::error title=change_id,file=openapi.json,col=6,endColumn=11,line=21,endLine=26::at openapi.yaml, in API GET /api/test This is a breaking change.\n" | ||
// assert.Equal(t, expectedOutput, string(output)) | ||
// } | ||
|
||
// func TestGitHubActionsFormatter_RenderBreakingChanges_JobOutputParameters(t *testing.T) { | ||
// // temp file to mock GITHUB_OUTPUT | ||
// tempFile, err := os.CreateTemp("", "github-output") | ||
// assert.NoError(t, err) | ||
// defer os.Remove(tempFile.Name()) | ||
// _ = os.Setenv("GITHUB_OUTPUT", tempFile.Name()) | ||
|
||
// testChanges := checker.Changes{ | ||
// checker.ApiChange{ | ||
// Id: "change_id", | ||
// Level: checker.ERR, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// checker.ApiChange{ | ||
// Id: "change_id", | ||
// Level: checker.ERR, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// checker.ApiChange{ | ||
// Id: "warning_id", | ||
// Level: checker.WARN, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// checker.ApiChange{ | ||
// Id: "notice_id", | ||
// Level: checker.INFO, | ||
// Operation: http.MethodGet, | ||
// Path: "/api/test", | ||
// Source: "openapi.yaml", | ||
// }, | ||
// } | ||
|
||
// // check output | ||
// output, err := gitHubFormatter.RenderBreakingChanges(testChanges, formatters.NewRenderOpts()) | ||
// assert.NoError(t, err) | ||
// _ = os.Unsetenv("GITHUB_OUTPUT") | ||
// expectedOutput := "::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n::error title=change_id::at openapi.yaml, in API GET /api/test This is a breaking change.\n::warning title=warning_id::at openapi.yaml, in API GET /api/test This is a warning.\n::notice title=notice_id::at openapi.yaml, in API GET /api/test This is a notice.\n" | ||
// assert.Equal(t, expectedOutput, string(output)) | ||
|
||
// // check job output parameters (NOTE: order of parameters is not guaranteed) | ||
// outputFile, err := os.ReadFile(tempFile.Name()) | ||
// assert.NoError(t, err) | ||
// assert.Contains(t, string(outputFile), "error_count=2\n") | ||
// assert.Contains(t, string(outputFile), "warning_count=1\n") | ||
// assert.Contains(t, string(outputFile), "info_count=1\n") | ||
// } | ||
|
||
// func TestGitHubActionsFormatter_NotImplemented(t *testing.T) { | ||
// var err error | ||
// _, err = gitHubFormatter.RenderDiff(nil, formatters.NewRenderOpts()) | ||
// assert.Error(t, err) | ||
|
||
// _, err = gitHubFormatter.RenderSummary(nil, formatters.NewRenderOpts()) | ||
// assert.Error(t, err) | ||
|
||
// _, err = gitHubFormatter.RenderChangelog(nil, formatters.NewRenderOpts(), nil) | ||
// assert.Error(t, err) | ||
|
||
// _, err = gitHubFormatter.RenderChecks(nil, formatters.NewRenderOpts()) | ||
// assert.Error(t, err) | ||
|
||
// _, err = gitHubFormatter.RenderFlatten(nil, formatters.NewRenderOpts()) | ||
// assert.Error(t, err) | ||
// } |