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

Fix line endings for CRLF (windows) #170

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitattributes
@@ -0,0 +1,5 @@
# test inputs should be platform depend
*.in.go text eol=auto

# formatted test results should always end on \n instead of \r\n
*.out.go text eol=lf
4 changes: 2 additions & 2 deletions pkg/config/config.go
@@ -1,7 +1,7 @@
package config

import (
"io/ioutil"
"os"
"sort"
"strings"

Expand Down Expand Up @@ -75,7 +75,7 @@ func (g YamlConfig) Parse() (*Config, error) {

func InitializeGciConfigFromYAML(filePath string) (*Config, error) {
config := YamlConfig{}
yamlData, err := ioutil.ReadFile(filePath)
yamlData, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
Expand Down
25 changes: 12 additions & 13 deletions pkg/gci/gci.go
Expand Up @@ -127,21 +127,23 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
return nil, nil, err
}

if cfg.SkipGenerated && parse.IsGeneratedFileByComment(string(src)) {
return src, src, nil
cleanedSrc := bytes.ReplaceAll(src, []byte(utils.WinLinebreak), []byte{utils.Linebreak})

if cfg.SkipGenerated && parse.IsGeneratedFileByComment(string(cleanedSrc)) {
return src, cleanedSrc, nil
}

imports, headEnd, tailStart, cStart, cEnd, err := parse.ParseFile(src, file.Path())
imports, headEnd, tailStart, cStart, cEnd, err := parse.ParseFile(cleanedSrc, file.Path())
if err != nil {
if errors.Is(err, parse.NoImportError{}) {
return src, src, nil
return src, cleanedSrc, nil
}
return nil, nil, err
}

// do not do format if only one import
if len(imports) <= 1 {
return src, src, nil
return src, cleanedSrc, nil
}

result, err := format.Format(imports, &cfg)
Expand All @@ -161,19 +163,19 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
}
for _, d := range result[s.String()] {
AddIndent(&body, &firstWithIndex)
body = append(body, src[d.Start:d.End]...)
body = append(body, cleanedSrc[d.Start:d.End]...)
}
}
}

head := make([]byte, headEnd)
copy(head, src[:headEnd])
tail := make([]byte, len(src)-tailStart)
copy(tail, src[tailStart:])
copy(head, cleanedSrc[:headEnd])
tail := make([]byte, len(cleanedSrc)-tailStart)
copy(tail, cleanedSrc[tailStart:])

// ensure C
if cStart != 0 {
head = append(head, src[cStart:cEnd]...)
head = append(head, cleanedSrc[cStart:cEnd]...)
head = append(head, utils.Linebreak)
}

Expand Down Expand Up @@ -202,9 +204,6 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
i += copy(dist[i:], s)
}

// remove ^M(\r\n) from Win to Unix
dist = bytes.ReplaceAll(dist, []byte{utils.WinLinebreak}, []byte{utils.Linebreak})

log.L().Debug(fmt.Sprintf("raw:\n%s", dist))
dist, err = goFormat.Source(dist)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions pkg/gci/gci_test.go
Expand Up @@ -2,7 +2,6 @@ package gci

import (
"os"
"runtime"
"strings"
"testing"

Expand All @@ -26,10 +25,6 @@ func isTestInputFile(_ string, file os.FileInfo) bool {
}

func TestRun(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows")
}

testFiles, err := io.FindFilesForPath(testFilesPath, isTestInputFile)
if err != nil {
t.Fatal(err)
Expand Down
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/already-good.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/blank-format.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-after-import.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-before-import.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-mixed.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-prefix.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-block-single-line.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-block.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-line.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-multiline.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/cgo-single.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/comment-before-import.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/comment-top.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/comment-with-slashslash.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/comment.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/leading-comment.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/multiple-imports.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/multiple-line-comment.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/no-format.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/nochar-after-import.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/nolint.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/number-in-alias.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/one-import-one-line.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/one-import.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/simple-case.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/whitespace-test.cfg.yaml
5 changes: 4 additions & 1 deletion pkg/gci/internal/testdata/with-comment-and-alias.cfg.yaml
1 change: 1 addition & 0 deletions pkg/section/newline_test.go
Expand Up @@ -11,6 +11,7 @@ func TestNewLineSpecificity(t *testing.T) {
{`""`, NewLine{}, specificity.MisMatch{}},
{`"x"`, NewLine{}, specificity.MisMatch{}},
{`"\n"`, NewLine{}, specificity.MisMatch{}},
{`"\r\n"`, NewLine{}, specificity.MisMatch{}},
}
testSpecificity(t, testCases)
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/utils/constants.go
@@ -1,9 +1,11 @@
package utils

const (
Indent = '\t'
Linebreak = '\n'
WinLinebreak = '\r'
Indent = '\t'

Linebreak = '\n'

WinLinebreak = "\r\n"

Colon = ":"

Expand Down