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

chore: Test on macos and windows. #75

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ on:

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

runs-on: ${{matrix.os}}

steps:
- uses: actions/checkout@v3

Expand Down
8 changes: 6 additions & 2 deletions goldens/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,20 @@ func TestGoldens(t *testing.T) {
t.Fatalf("Could not read .err file: %v", err)
}
}
// stderr should only ever use "\n" for line endings, but the golden
// file we read might have OS-specific line endings thanks to Git.
wantErr = []byte(strings.ReplaceAll(string(wantErr), "\r\n", "\n"))
wantErr = []byte(strings.ReplaceAll(string(wantErr), "\r", "\n"))

gotOut, gotErr, err := runKeepSorted(in)
if err != nil {
t.Errorf("Had trouble running keep-sorted: %v", err)
}
if diff := cmp.Diff(strings.Split(string(wantOut), "\n"), strings.Split(gotOut, "\n")); diff != "" {
if diff := cmp.Diff(string(wantOut), gotOut); diff != "" {
t.Errorf("keep-sorted stdout diff (-want +got):\n%s", diff)
needsRegen <- inFile
}
if diff := cmp.Diff(strings.Split(string(wantErr), "\n"), strings.Split(gotErr, "\n")); diff != "" {
if diff := cmp.Diff(string(wantErr), gotErr); diff != "" {
t.Errorf("keep-sorted stderr diff (-want +got):\n%s", diff)
needsRegen <- inFile
}
Expand Down
29 changes: 20 additions & 9 deletions keepsorted/keep_sorted.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func New(id string, defaultOptions BlockOptions) *Fixer {

// Fix all of the findings on contents to make keep-sorted happy.
func (f *Fixer) Fix(filename, contents string, modifiedLines []LineRange) (fixed string, alreadyCorrect bool, warnings []*Finding) {
lines := strings.Split(contents, "\n")
findings := f.findings(filename, lines, modifiedLines)
lines, ending := lines(contents)
findings := f.findings(filename, lines, ending, modifiedLines)
if len(findings) == 0 {
return contents, true, nil
}
Expand Down Expand Up @@ -83,23 +83,34 @@ func (f *Fixer) Fix(filename, contents string, modifiedLines []LineRange) (fixed
endLine := repl.Lines.Start

// -1 to convert line number to index number.
s.WriteString(linesToString(lines[startLine-1 : endLine-1]))
s.WriteString(linesToString(lines[startLine-1:endLine-1], ending))
s.WriteString(repl.NewContent)

startLine = repl.Lines.End + 1
}
s.WriteString(strings.Join(lines[startLine-1:], "\n"))
s.WriteString(strings.Join(lines[startLine-1:], ending))

return s.String(), false, warnings
}

func lines(s string) (lines []string, ending string) {
for _, ending := range []string{"\r\n", "\r"} {
if strings.Contains(s, ending) {
return strings.Split(s, ending), ending
}
}

return strings.Split(s, "\n"), "\n"
}

// Findings returns a slice of things that need to be addressed in the file to
// make keep-sorted happy.
//
// If modifiedLines is non-nil, we only report findings for issues within the
// modified lines. Otherwise, we report all findings.
func (f *Fixer) Findings(filename, contents string, modifiedLines []LineRange) []*Finding {
return f.findings(filename, strings.Split(contents, "\n"), modifiedLines)
lines, ending := lines(contents)
return f.findings(filename, lines, ending, modifiedLines)
}

// Finding is something that keep-sorted thinks is wrong with a particular file.
Expand Down Expand Up @@ -142,7 +153,7 @@ type Replacement struct {
NewContent string `json:"new_content"`
}

func (f *Fixer) findings(filename string, contents []string, modifiedLines []LineRange) []*Finding {
func (f *Fixer) findings(filename string, contents []string, ending string, modifiedLines []LineRange) []*Finding {
blocks, incompleteBlocks, warns := f.newBlocks(filename, contents, 1, includeModifiedLines(modifiedLines))

var fs []*Finding
Expand All @@ -164,7 +175,7 @@ func (f *Fixer) findings(filename string, contents []string, modifiedLines []Lin

for _, b := range blocks {
if s, alreadySorted := b.sorted(); !alreadySorted {
repl := replacement(b.start+1, b.end-1, linesToString(s))
repl := replacement(b.start+1, b.end-1, linesToString(s, ending))
// Only try to automatically sort things if there are no incomplete blocks.
repl.automatic = len(incompleteBlocks) == 0
fs = append(fs, finding(filename, b.start+1, b.end-1, errorUnordered, repl))
Expand Down Expand Up @@ -195,8 +206,8 @@ func includeModifiedLines(modifiedLines []LineRange) func(start, end int) bool {
// linesToString converts the string slice of lines into a single string.
// This function assumes that every line should end with "\n", including the
// last line.
func linesToString(lines []string) string {
return strings.Join(lines, "\n") + "\n"
func linesToString(lines []string, ending string) string {
return strings.Join(lines, ending) + ending
}

func finding(filename string, start, end int, msg string, fixes ...Fix) *Finding {
Expand Down
2 changes: 1 addition & 1 deletion keepsorted/keep_sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ baz
mod = append(mod, LineRange{l, l})
}
}
got := New("keep-sorted-test", BlockOptions{}).findings(filename, strings.Split(tc.in, "\n"), mod)
got := New("keep-sorted-test", BlockOptions{}).findings(filename, strings.Split(tc.in, "\n"), "\n", mod)
if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(Fix{})); diff != "" {
t.Errorf("Findings diff (-want +got):\n%s", diff)
}
Expand Down
Loading