Skip to content

Commit 6e1860f

Browse files
committed
chore: move conversion into helper
1 parent 187f835 commit 6e1860f

File tree

4 files changed

+30
-33
lines changed

4 files changed

+30
-33
lines changed

bundle/bundle_manager.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package bundle
1818

1919
import (
20-
"bytes"
2120
"context"
2221
"os"
2322
"path/filepath"
@@ -113,14 +112,14 @@ func (b *bundleManager) Create(ctx context.Context,
113112
b.logger.Error().Err(err).Str("filePath", absoluteFilePath).Msg("could not load content of file")
114113
continue
115114
}
116-
var fileContent []byte
117-
fileContent, err = util.ConvertToUTF8(bytes.NewReader(rawContent))
118-
if err != nil {
119-
b.logger.Error().Err(err).Str("filePath", absoluteFilePath).Msg("could not convert content of file to UTF-8")
115+
116+
bundleFile, bundleError := deepcode.BundleFileFrom(rawContent)
117+
if bundleError != nil {
118+
b.logger.Error().Err(bundleError).Str("filePath", absoluteFilePath).Msg("could not convert content of file to UTF-8")
120119
continue
121120
}
122121

123-
if !(len(fileContent) > 0 && len(fileContent) <= maxFileSize) {
122+
if !(len(bundleFile.Content) > 0 && len(bundleFile.Content) <= maxFileSize) {
124123
continue
125124
}
126125

@@ -131,7 +130,6 @@ func (b *bundleManager) Create(ctx context.Context,
131130
}
132131
relativePath = util.EncodePath(relativePath)
133132

134-
bundleFile := deepcode.BundleFileFrom(fileContent)
135133
bundleFiles[relativePath] = bundleFile
136134
fileHashes[relativePath] = bundleFile.Hash
137135
b.logger.Trace().Str("method", "BundleFileFrom").Str("hash", bundleFile.Hash).Str("filePath", absoluteFilePath).Msg("")

bundle/bundle_test.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717
package bundle_test
1818

1919
import (
20-
"bytes"
2120
"context"
2221
"crypto/sha256"
2322
"encoding/hex"
24-
"io"
2523
"os"
2624
"testing"
2725

2826
"github.com/golang/mock/gomock"
2927
"github.com/rs/zerolog"
3028
"github.com/stretchr/testify/assert"
3129
"github.com/stretchr/testify/require"
32-
"golang.org/x/net/html/charset"
3330

3431
"github.com/snyk/code-client-go/bundle"
3532
"github.com/snyk/code-client-go/internal/deepcode"
@@ -114,32 +111,21 @@ func Test_UploadBatch(t *testing.T) {
114111
func Test_BundleEncoding(t *testing.T) {
115112
t.Run("utf-8 encoded content", func(t *testing.T) {
116113
content := []byte("hello")
117-
bundle := deepcode.BundleFileFrom(content)
118-
119-
utf8Reader, err := charset.NewReaderLabel("UTF-8", bytes.NewReader([]byte(bundle.Content)))
120-
assert.NoError(t, err)
121-
122-
actualContent, err := io.ReadAll(utf8Reader)
114+
bundle, err := deepcode.BundleFileFrom(content)
123115
assert.NoError(t, err)
124116

125-
actualShasum := sha256.Sum256(actualContent)
117+
actualShasum := sha256.Sum256([]byte(bundle.Content))
126118
assert.Equal(t, bundle.Hash, hex.EncodeToString(actualShasum[:]))
127119
})
128120

129121
t.Run("non utf-8 / binary file", func(t *testing.T) {
130122
content, err := os.ReadFile("testdata/rshell_font.php")
131123
assert.NoError(t, err)
132124

133-
bundle := deepcode.BundleFileFrom(content)
134-
135-
utf8Reader, err := charset.NewReaderLabel("UTF-8", bytes.NewReader(content))
136-
assert.NoError(t, err)
137-
138-
actualContent, err := io.ReadAll(utf8Reader)
125+
bundle, err := deepcode.BundleFileFrom(content)
139126
assert.NoError(t, err)
140127

141-
actualHash := sha256.Sum256(actualContent)
142-
143-
assert.Equal(t, bundle.Hash, hex.EncodeToString(actualHash[:]))
128+
actualShasum := sha256.Sum256([]byte(bundle.Content))
129+
assert.Equal(t, bundle.Hash, hex.EncodeToString(actualShasum[:]))
144130
})
145131
}

internal/deepcode/helpers.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package deepcode
1717

1818
import (
19+
"bytes"
20+
1921
"github.com/snyk/code-client-go/internal/util"
2022
)
2123

@@ -24,10 +26,15 @@ type BundleFile struct {
2426
Content string `json:"content"`
2527
}
2628

27-
func BundleFileFrom(content []byte) BundleFile {
29+
func BundleFileFrom(rawContent []byte) (BundleFile, error) {
30+
fileContent, err := util.ConvertToUTF8(bytes.NewReader(rawContent))
31+
if err != nil {
32+
return BundleFile{}, err
33+
}
34+
2835
file := BundleFile{
29-
Hash: util.Hash(content),
30-
Content: string(content),
36+
Hash: util.Hash(rawContent),
37+
Content: string(fileContent),
3138
}
32-
return file
39+
return file, nil
3340
}

scan_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ package codeclient_test
1717

1818
import (
1919
"context"
20-
"github.com/google/uuid"
2120
"os"
2221
"path/filepath"
2322
"testing"
2423

24+
"github.com/google/uuid"
25+
2526
"github.com/golang/mock/gomock"
2627
"github.com/rs/zerolog"
2728
"github.com/stretchr/testify/assert"
@@ -44,9 +45,14 @@ import (
4445
func Test_UploadAndAnalyze(t *testing.T) {
4546
baseDir, firstDocPath, secondDocPath, firstDocContent, secondDocContent := setupDocs(t)
4647
docs := sliceToChannel([]string{firstDocPath, secondDocPath})
48+
firstBundle, err := deepcode.BundleFileFrom(firstDocContent)
49+
assert.NoError(t, err)
50+
secondBundle, err := deepcode.BundleFileFrom(secondDocContent)
51+
assert.NoError(t, err)
52+
4753
files := map[string]deepcode.BundleFile{
48-
firstDocPath: deepcode.BundleFileFrom(firstDocContent),
49-
firstDocPath: deepcode.BundleFileFrom(secondDocContent),
54+
firstDocPath: firstBundle,
55+
firstDocPath: secondBundle,
5056
}
5157

5258
logger := zerolog.Nop()

0 commit comments

Comments
 (0)