Skip to content

Commit

Permalink
fix unintended modification of source when parsing input file (#126)
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Hofstetter <[email protected]>

Signed-off-by: Marco Hofstetter <[email protected]>
  • Loading branch information
mhofstetter authored Nov 17, 2022
1 parent 6d0b9f7 commit fafa02a
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 fafa02a

Please sign in to comment.