-
-
Notifications
You must be signed in to change notification settings - Fork 110
Export/Results as CSV #519
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
Open
stelgenhof
wants to merge
3
commits into
raviqqe:main
Choose a base branch
from
stelgenhof:feature-csv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| type csvPageResult struct { | ||
| URL string | ||
| Links []csvLinkResult | ||
| } | ||
|
|
||
| type csvLinkResult struct { | ||
| URL string | ||
| Status string | ||
| } | ||
|
|
||
| func newCSVPageResult(r *pageResult, verbose bool) *csvPageResult { | ||
| c := len(r.ErrorLinkResults) | ||
|
|
||
| if verbose { | ||
| c += len(r.SuccessLinkResults) | ||
| } | ||
|
|
||
| ls := make([]csvLinkResult, 0, c) | ||
|
|
||
| if verbose { | ||
| for _, r := range r.SuccessLinkResults { | ||
| ls = append(ls, csvLinkResult{ | ||
| URL: r.URL, | ||
| Status: strconv.Itoa(r.StatusCode), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| for _, r := range r.ErrorLinkResults { | ||
| ls = append(ls, csvLinkResult{ | ||
| URL: r.URL, | ||
| Status: r.Error.Error(), | ||
| }) | ||
| } | ||
|
|
||
| return &csvPageResult{r.URL, ls} | ||
| } | ||
|
|
||
| func (r *csvPageResult) String() string { | ||
| var buf strings.Builder | ||
|
|
||
| buf.WriteString(`"Page URL","Link URL",Status` + "\n") | ||
|
|
||
| for _, link := range r.Links { | ||
| buf.WriteString(`"` + strings.ReplaceAll(r.URL, `"`, `""`) + `","` + | ||
| strings.ReplaceAll(link.URL, `"`, `""`) + `",` + | ||
| link.Status + "\n") | ||
| } | ||
|
|
||
| return buf.String() | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNewCSVPageResult(t *testing.T) { | ||
| r := &pageResult{ | ||
| URL: "http://foo.com", | ||
| SuccessLinkResults: []*successLinkResult{ | ||
| {URL: "http://foo.com/success", StatusCode: 200}, | ||
| }, | ||
| ErrorLinkResults: []*errorLinkResult{ | ||
| {URL: "http://foo.com/error", Error: errors.New("404")}, | ||
| }, | ||
| } | ||
|
|
||
| t.Run("verbose mode", func(t *testing.T) { | ||
| result := newCSVPageResult(r, true) | ||
|
|
||
| if result.URL != "http://foo.com" { | ||
| t.Errorf("expected URL to be 'http://foo.com', got '%s'", result.URL) | ||
| } | ||
|
|
||
| if len(result.Links) != 2 { | ||
| t.Errorf("expected 2 links, got %d", len(result.Links)) | ||
| } | ||
|
|
||
| successLink := result.Links[0] | ||
| if successLink.URL != "http://foo.com/success" { | ||
| t.Errorf("expected success URL to be 'http://foo.com/success', got '%s'", successLink.URL) | ||
| } | ||
| if successLink.Status != "200" { | ||
| t.Errorf("expected status to be '200', got '%s'", successLink.Status) | ||
| } | ||
|
|
||
| errorLink := result.Links[1] | ||
| if errorLink.URL != "http://foo.com/error" { | ||
| t.Errorf("expected error URL to be 'http://foo.com/error', got '%s'", errorLink.URL) | ||
| } | ||
| if errorLink.Status != "404" { | ||
| t.Errorf("expected status to be '404', got '%s'", errorLink.Status) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("non-verbose mode", func(t *testing.T) { | ||
| result := newCSVPageResult(r, false) | ||
|
|
||
| if len(result.Links) != 1 { | ||
| t.Errorf("expected 1 link, got %d", len(result.Links)) | ||
| } | ||
|
|
||
| errorLink := result.Links[0] | ||
| if errorLink.URL != "http://foo.com/error" { | ||
| t.Errorf("expected error URL to be 'http://foo.com/error', got '%s'", errorLink.URL) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestCSVPageResultString(t *testing.T) { | ||
| result := &csvPageResult{ | ||
| URL: "http://foo.com", | ||
| Links: []csvLinkResult{ | ||
| {URL: "http://foo.com/success", Status: "200"}, | ||
| {URL: "http://foo.com/error", Status: "404"}, | ||
| }, | ||
| } | ||
|
|
||
| output := result.String() | ||
| lines := strings.Split(strings.TrimSpace(output), "\n") | ||
|
|
||
| if len(lines) != 3 { | ||
| t.Errorf("expected 3 lines (header + 2 data), got %d", len(lines)) | ||
| } | ||
|
|
||
| expectedHeader := `"Page URL","Link URL",Status` | ||
| if lines[0] != expectedHeader { | ||
| t.Errorf("expected header '%s', got '%s'", expectedHeader, lines[0]) | ||
| } | ||
|
|
||
| expectedRow1 := `"http://foo.com","http://foo.com/success",200` | ||
| if lines[1] != expectedRow1 { | ||
| t.Errorf("expected first row '%s', got '%s'", expectedRow1, lines[1]) | ||
| } | ||
|
|
||
| expectedRow2 := `"http://foo.com","http://foo.com/error",404` | ||
| if lines[2] != expectedRow2 { | ||
| t.Errorf("expected second row '%s', got '%s'", expectedRow2, lines[2]) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the
encoding/csvpackage?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to be accurate and ensure that string fields are always quoted, since URL's technically can have comma's (as per RFC 3986). For example in the query string. My first attempt was actually using the
encode/csvpackage, however read somewhere that it doesn't quote by default or otherwise quote every field, so opted for this approach.However, after some more reading, I think my understanding may have been wrong and can actually use the
encoding/csvinstead. I'll do some refactoring 😄