You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-09-16 09:26:22 +02:00
fix: whitespace
This commit is contained in:
@@ -107,10 +107,10 @@ func handleEncryption(password string, data []byte) ([]byte, error) {
|
||||
|
||||
func parseInput(data []byte) (piperenv.CPEMap, error) {
|
||||
commonPipelineEnv := piperenv.CPEMap{}
|
||||
|
||||
|
||||
// Clean invalid UTF-8 sequences that can cause JSON parsing errors
|
||||
cleanData := piperenv.CleanJSONData(data)
|
||||
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewReader(cleanData))
|
||||
decoder.UseNumber()
|
||||
if err := decoder.Decode(&commonPipelineEnv); err != nil {
|
||||
@@ -119,7 +119,6 @@ func parseInput(data []byte) (piperenv.CPEMap, error) {
|
||||
return commonPipelineEnv, nil
|
||||
}
|
||||
|
||||
|
||||
func writeOutput(commonPipelineEnv piperenv.CPEMap) (int, error) {
|
||||
rootPath := filepath.Join(GeneralConfig.EnvRootPath, "commonPipelineEnvironment")
|
||||
if err := commonPipelineEnv.WriteToDisk(rootPath); err != nil {
|
||||
|
@@ -123,10 +123,10 @@ func readFileContent(fullPath string) (string, interface{}, bool, error) {
|
||||
if strings.HasSuffix(fullPath, ".json") {
|
||||
// value is json encoded
|
||||
var value interface{}
|
||||
|
||||
|
||||
// Clean invalid UTF-8 sequences that can cause JSON parsing errors
|
||||
cleanContent := CleanJSONData(fileContent)
|
||||
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewReader(cleanContent))
|
||||
decoder.UseNumber()
|
||||
err = decoder.Decode(&value)
|
||||
@@ -147,18 +147,18 @@ func CleanJSONData(data []byte) []byte {
|
||||
if !utf8.Valid(data) {
|
||||
data = []byte(strings.ToValidUTF8(string(data), "\uFFFD"))
|
||||
}
|
||||
|
||||
|
||||
// Check if it's already valid JSON - if so, return as-is
|
||||
if json.Valid(data) {
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
// If not valid JSON, try to escape control characters in string literals
|
||||
s := string(data)
|
||||
result := strings.Builder{}
|
||||
inString := false
|
||||
escaped := false
|
||||
|
||||
|
||||
for _, r := range s {
|
||||
if !inString {
|
||||
result.WriteRune(r)
|
||||
@@ -167,25 +167,25 @@ func CleanJSONData(data []byte) []byte {
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if escaped {
|
||||
result.WriteRune(r)
|
||||
escaped = false
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if r == '\\' {
|
||||
escaped = true
|
||||
result.WriteRune(r)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if r == '"' {
|
||||
inString = false
|
||||
result.WriteRune(r)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Handle control characters (0x00-0x1F) that are invalid in JSON strings
|
||||
if r < 0x20 {
|
||||
switch r {
|
||||
@@ -207,6 +207,6 @@ func CleanJSONData(data []byte) []byte {
|
||||
result.WriteRune(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return []byte(result.String())
|
||||
}
|
||||
|
@@ -110,50 +110,50 @@ func TestCommonPipelineEnvDirNotPresent(t *testing.T) {
|
||||
|
||||
func TestCleanJSONData(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
// Test valid UTF-8 (should be unchanged)
|
||||
validData := []byte(`{"commitMessage":"This is a valid commit message"}`)
|
||||
result := CleanJSONData(validData)
|
||||
require.Equal(t, validData, result)
|
||||
|
||||
|
||||
// Test emoji with valid UTF-8 (should be unchanged)
|
||||
emojiData := []byte(`{"commitMessage":"🚀 feat: add new feature"}`)
|
||||
result = CleanJSONData(emojiData)
|
||||
require.Equal(t, emojiData, result)
|
||||
|
||||
|
||||
// Test data with JSON control character (like \x16)
|
||||
invalidData := []byte("{\"commitMessage\":\"Test \x16 invalid char\"}")
|
||||
result = CleanJSONData(invalidData)
|
||||
require.NotEqual(t, invalidData, result)
|
||||
require.True(t, json.Valid(result), "Result should be valid JSON")
|
||||
|
||||
|
||||
// Verify we can parse the cleaned data as JSON
|
||||
var parsed map[string]interface{}
|
||||
err := json.Unmarshal(result, &parsed)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, parsed, "commitMessage")
|
||||
|
||||
|
||||
// The control character should be escaped as unicode
|
||||
require.Contains(t, string(result), "\\u0016")
|
||||
}
|
||||
|
||||
func TestReadFileContentWithInvalidUTF8(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
// Create a temporary JSON file with control characters
|
||||
tmpDir := t.TempDir()
|
||||
jsonFile := path.Join(tmpDir, "test.json")
|
||||
|
||||
// Write JSON with control character that causes parsing errors
|
||||
|
||||
// Write JSON with control character that causes parsing errors
|
||||
invalidJSON := []byte("{\"commitMessage\":\"Test \x16 control char commit\"}")
|
||||
err := os.WriteFile(jsonFile, invalidJSON, 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
// Try to read the file - should not fail due to control characters
|
||||
_, value, _, err := readFileContent(jsonFile)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, value)
|
||||
|
||||
|
||||
// Verify we can extract the commit message
|
||||
if valueMap, ok := value.(map[string]interface{}); ok {
|
||||
commitMsg, exists := valueMap["commitMessage"]
|
||||
|
Reference in New Issue
Block a user