Skip to content

Commit

Permalink
fix unintended modification of source when parsing input file
Browse files Browse the repository at this point in the history
  • Loading branch information
mhofstetter committed Nov 16, 2022
1 parent 1887cca commit ff02198
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 4 additions & 2 deletions pkg/gci/gci.go
Expand Up @@ -156,8 +156,10 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
}
}

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

head = append(head, utils.Linebreak)
// add beginning of import block
Expand Down
12 changes: 9 additions & 3 deletions pkg/gci/gci_test.go
@@ -1,13 +1,13 @@
package gci

import (
"io/ioutil"
"os"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/daixiang0/gci/pkg/config"
"github.com/daixiang0/gci/pkg/io"
Expand Down Expand Up @@ -44,11 +44,17 @@ func TestRun(t *testing.T) {
t.Fatal(err)
}

_, formattedFile, err := LoadFormatGoFile(io.File{FilePath: fileBaseName + ".in.go"}, *gciCfg)
inputSrcFile := io.File{FilePath: fileBaseName + ".in.go"}
inputSrc, err := inputSrcFile.Load()
require.NoError(t, err)

unmodifiedFile, formattedFile, err := LoadFormatGoFile(inputSrcFile, *gciCfg)
if err != nil {
t.Fatal(err)
}
expectedOutput, err := ioutil.ReadFile(fileBaseName + ".out.go")
assert.Equal(t, inputSrc, unmodifiedFile)

expectedOutput, err := os.ReadFile(fileBaseName + ".out.go")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit ff02198

Please sign in to comment.