Skip to content

Commit

Permalink
Fix line endings bug on Windows
Browse files Browse the repository at this point in the history
This commit allows to properly parse package names, 
regardless of line endings (`\r\n` or `\n`) used in the 
`go.mod` file.
  • Loading branch information
phanirithvij authored Mar 8, 2022
1 parent a79d558 commit 02e40ff
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto eol=lf
*.golden text eol=lf
8 changes: 6 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ on:
jobs:
test:
name: Test
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Set up Go 1.17
uses: actions/setup-go@v1
Expand All @@ -17,9 +20,10 @@ jobs:
uses: actions/checkout@v2
- name: Install go-task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Run tests
run: task test
- uses: codecov/codecov-action@v2
with:
files: ./coverage.out

21 changes: 18 additions & 3 deletions internal/gocovshtest/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package gocovshtest

import (
"runtime"
"testing"

tea "github.com/charmbracelet/bubbletea"
"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/require"
)

func isWindows() bool { return runtime.GOOS == "windows" }

func TestErrorFlows(t *testing.T) {
g := goldie.New(t, goldie.WithFixtureDir("testdata/errors"))

Expand All @@ -27,7 +30,11 @@ func TestErrorFlows(t *testing.T) {
mm, cmd = mt.sendErrorMsg(initMsg)
require.NotNil(t, mm)
require.Nil(t, cmd)
g.Assert(t, "error_flows_missing_coverage_file", []byte(mm.View()))
if isWindows() {
g.Assert(t, "error_flows_missing_coverage_file_windows", []byte(mm.View()))
} else {
g.Assert(t, "error_flows_missing_coverage_file", []byte(mm.View()))
}

mm, cmd = mt.sendLetterKey('f')
require.NotNil(t, mm)
Expand All @@ -54,7 +61,11 @@ func TestErrorFlows(t *testing.T) {
mm, cmd = mt.sendErrorMsg(initMsg)
require.NotNil(t, mm)
require.Nil(t, cmd)
g.Assert(t, "error_flows_missing_go.mod_file", []byte(mm.View()))
if isWindows() {
g.Assert(t, "error_flows_missing_go.mod_file_windows", []byte(mm.View()))
} else {
g.Assert(t, "error_flows_missing_go.mod_file", []byte(mm.View()))
}
})

t.Run("invalid coverage file", func(t *testing.T) {
Expand Down Expand Up @@ -122,7 +133,11 @@ func TestErrorFlows(t *testing.T) {
mm, cmd = mt.sendErrorMsg(errMsg)
require.NotNil(t, mm)
require.Nil(t, cmd)
g.Assert(t, "error_flows_missing_source_file", []byte(mm.View()))
if isWindows() {
g.Assert(t, "error_flows_missing_source_file_windows", []byte(mm.View()))
} else {
g.Assert(t, "error_flows_missing_source_file", []byte(mm.View()))
}
})

t.Run("invalid go.mod", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

Requested coverage profile is not found.
By default, "coverage.out" is used. For other names, use "--profile" flag.

The original error was:
open testdata/general/missing.cover: no such file or directory
The original error was:
open testdata/general/missing.cover: The system cannot find the file specified.

Press any key to exit

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

go.mod file is not available

This program must be executed from Go project root.
Run the program from the folder that contains go.mod file.

The original error was:
failed to determine package name: open testdata/no-go.mod/go.mod: The system cannot find the path specified.

Press any key to exit

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

Source code file not found

A file that appears in the coverage report was not found in the file tree.
Update the coverage report and try again.

The original error was:
open testdata/errors/invalid_file.go: The system cannot find the file specified.

Press any key to exit

4 changes: 3 additions & 1 deletion internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ func determinePackageName(gomodFile string) (string, error) {
return "", errGoModNotFound{err}
}

matches := modulePattern.FindStringSubmatch(string(bs))
content := strings.ReplaceAll(string(bs), "\r\n", "\n")
matches := modulePattern.FindStringSubmatch(content)

if len(matches) == 0 {
return "", errInvalidGoMod{}
}
Expand Down
6 changes: 6 additions & 0 deletions internal/program/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -55,4 +56,9 @@ func TestLogger(t *testing.T) {
logs, err := os.ReadFile(f.Name())
require.NoError(t, err)
require.Contains(t, string(logs), "logging to")

if runtime.GOOS == "windows" {
// https://github.com/docker/compose/issues/8186#issuecomment-814180124
t.Log("Workaround for containerd/console bug")
}
}

0 comments on commit 02e40ff

Please sign in to comment.