Skip to content
51 changes: 49 additions & 2 deletions cmd/pythonBuild.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd
1package cmd

Check failure on line 1 in cmd/pythonBuild.go

View workflow job for this annotation

GitHub Actions / format

'p' exponent requires hexadecimal mantissa

Check failure on line 1 in cmd/pythonBuild.go

View workflow job for this annotation

GitHub Actions / unit

'p' exponent requires hexadecimal mantissa

Check failure on line 1 in cmd/pythonBuild.go

View workflow job for this annotation

GitHub Actions / unit

'p' exponent requires hexadecimal mantissa

Check failure on line 1 in cmd/pythonBuild.go

View workflow job for this annotation

GitHub Actions / generate

'p' exponent requires hexadecimal mantissa

Check failure on line 1 in cmd/pythonBuild.go

View workflow job for this annotation

GitHub Actions / lint

'p' exponent requires hexadecimal mantissa (compile)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/SAP/jenkins-library/pkg/buildsettings"
"github.com/SAP/jenkins-library/pkg/command"
Expand Down Expand Up @@ -85,9 +88,53 @@
return fmt.Errorf("failed to publish: %w", err)
}
}

// After build, rename all dist/* files with underscores to dashes in the base name
// This was introduced to fix setuptools renaming from "-" to "_"
const distDir = "dist" // move to config or ?
renameArtifactsInDist(distDir)

return nil
}

func renameArtifactsInDist(distDir string) {
files, err := os.ReadDir(distDir)
if err != nil {
log.Entry().Warnf("Could not read dist directory for artifact renaming: %v", err)
return
}

for _, f := range files {
oldName := f.Name()
// Only process .tar.gz artifacts with at least one underscore
if !strings.Contains(oldName, "_") || !strings.HasSuffix(oldName, ".tar.gz") {
continue
}

// Remove .tar.gz extension for processing
base := strings.TrimSuffix(oldName, ".tar.gz")
lastUnderscore := strings.LastIndex(base, "_")
if lastUnderscore == -1 {
continue
}
namePart := base[:lastUnderscore]
versionPart := base[lastUnderscore+1:]
// Replace underscores with dashes only in the version part
newVersionPart := strings.ReplaceAll(versionPart, "_", "-")
newName := namePart + "_" + newVersionPart + ".tar.gz"
if newName == oldName {
continue
}
oldPath := filepath.Join(distDir, oldName)
newPath := filepath.Join(distDir, newName)
if err := os.Rename(oldPath, newPath); err != nil {
log.Entry().Warnf("Failed to rename artifact %s to %s: %v", oldName, newName, err)
} else {
log.Entry().Infof("Renamed artifact %s to %s", oldName, newName)
}
}
}

// TODO: extract to common place
func createBuildSettingsInfo(config *pythonBuildOptions) (string, error) {
log.Entry().Debugf("creating build settings information...")
Expand All @@ -104,7 +151,7 @@
}
buildSettingsInfo, err := buildsettings.CreateBuildSettingsInfo(&pythonConfig, stepName)
if err != nil {
log.Entry().Warnf("failed to create build settings info: %v", err)
return "", err
}
return buildSettingsInfo, nil
}
86 changes: 86 additions & 0 deletions cmd/pythonBuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"fmt"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -109,3 +110,88 @@ func TestRunPythonBuild(t *testing.T) {
assert.Equal(t, []string{"env", "--output-file", "bom-pip.xml", "--output-format", "XML", "--spec-version", "1.4"}, utils.ExecMockRunner.Calls[5].Params)
})
}

func Test_renameArtifactsInDistM(t *testing.T) {
// Define test cases
tests := []struct {
testName string
inputFilename string
expectedFilename string
shouldRename bool // Whether renaming is expected
}{
{
testName: "Rename file with underscore in the name",
inputFilename: "test_artifact_1.0.0.tar.gz",
expectedFilename: "test-artifact-1.0.0.tar.gz",
shouldRename: true,
},
{
testName: "Rename file with multiple underscores",
inputFilename: "another_test_artifact_1_0_0.tar.gz",
expectedFilename: "another-test-artifact-1-0-0.tar.gz",
shouldRename: true,
},
{
testName: "Do not rename file without underscore",
inputFilename: "test-artifact-1.0.0.tar.gz",
expectedFilename: "test-artifact-1.0.0.tar.gz",
shouldRename: false,
},
{
testName: "Ignore non-Python artifact file (ZIP)",
inputFilename: "random_file_1.0.0.zip",
expectedFilename: "random_file_1.0.0.zip",
shouldRename: false,
},
}

// Create a temporary directory for testing
distTempDir := t.TempDir()

// Create test files in the temporary directory
for _, tt := range tests {
filePath := filepath.Join(distTempDir, tt.inputFilename)
if err := os.WriteFile(filePath, []byte("sample"), 0644); err != nil {
t.Fatalf("Failed to create test file %s: %v", tt.inputFilename, err)
}
}

// Run the function under test
renameArtifactsInDist(distTempDir)

// Validate the results
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
oldPath := filepath.Join(distTempDir, tt.inputFilename)
newPath := filepath.Join(distTempDir, tt.expectedFilename)

if tt.inputFilename == tt.expectedFilename {
// Special case: No renaming expected
if _, err := os.Stat(oldPath); os.IsNotExist(err) {
t.Errorf("Expected file %s to remain, but it does not exist", oldPath)
}
return
}

// General case: Renaming logic
_, oldExistsErr := os.Stat(oldPath)
_, newExistsErr := os.Stat(newPath)

if tt.shouldRename {
if oldExistsErr == nil {
t.Errorf("Expected old file %s to be renamed, but it still exists", oldPath)
}
if os.IsNotExist(newExistsErr) {
t.Errorf("Expected new file %s to exist, but it does not", newPath)
}
} else {
if os.IsNotExist(oldExistsErr) {
t.Errorf("Expected file %s to remain, but it does not exist", oldPath)
}
if newExistsErr == nil {
t.Errorf("Expected file %s not to be renamed, but it was renamed to %s", tt.inputFilename, tt.expectedFilename)
}
}
})
}
}
Loading