Skip to content

Commit fafa02a

Browse files
authored
fix unintended modification of source when parsing input file (#126)
Signed-off-by: Marco Hofstetter <[email protected]> Signed-off-by: Marco Hofstetter <[email protected]>
1 parent 6d0b9f7 commit fafa02a

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pkg/gci/gci.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ func LoadFormatGoFile(file io.FileObj, cfg config.Config) (src, dist []byte, err
156156
}
157157
}
158158

159-
head := src[:headEnd]
160-
tail := src[tailStart:]
159+
head := make([]byte, headEnd)
160+
copy(head, src[:headEnd])
161+
tail := make([]byte, len(src)-tailStart)
162+
copy(tail, src[tailStart:])
161163

162164
head = append(head, utils.Linebreak)
163165
// add beginning of import block

pkg/gci/gci_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package gci
22

33
import (
4-
"io/ioutil"
54
"os"
65
"runtime"
76
"strings"
87
"testing"
98

109
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1111

1212
"github.com/daixiang0/gci/pkg/config"
1313
"github.com/daixiang0/gci/pkg/io"
@@ -44,11 +44,17 @@ func TestRun(t *testing.T) {
4444
t.Fatal(err)
4545
}
4646

47-
_, formattedFile, err := LoadFormatGoFile(io.File{FilePath: fileBaseName + ".in.go"}, *gciCfg)
47+
inputSrcFile := io.File{FilePath: fileBaseName + ".in.go"}
48+
inputSrc, err := inputSrcFile.Load()
49+
require.NoError(t, err)
50+
51+
unmodifiedFile, formattedFile, err := LoadFormatGoFile(inputSrcFile, *gciCfg)
4852
if err != nil {
4953
t.Fatal(err)
5054
}
51-
expectedOutput, err := ioutil.ReadFile(fileBaseName + ".out.go")
55+
assert.Equal(t, inputSrc, unmodifiedFile)
56+
57+
expectedOutput, err := os.ReadFile(fileBaseName + ".out.go")
5258
if err != nil {
5359
t.Fatal(err)
5460
}

0 commit comments

Comments
 (0)