fix bug in Version: line; add "selftest patchwhl" command + acceptance test

This commit is contained in:
Denis Bilenko 2025-03-05 15:18:59 +01:00
parent 5c46e62374
commit edf361ce6c
15 changed files with 84 additions and 60 deletions

View File

@ -0,0 +1,6 @@
=== Test prebuilt wheel:
>>> setmtime.py 2025-03-05 15:07:33.123456789 my_test_code-0.0.1-py3-none-any.whl
>>> [CLI] selftest patchwhl my_test_code-0.0.1-py3-none-any.whl
Warn: Patched whl: my_test_code-0.0.1-py3-none-any.whl -> my_test_code-0.0.1+2025030514073312-py3-none-any.whl

View File

@ -0,0 +1,9 @@
Metadata-Version: 2.1
Name: my-test-code
Version: 0.0.1+2025030514073312
Summary: my test wheel
Home-page: https://databricks.com
Author: Databricks
Author-email: john.doe@databricks.com
Requires-Dist: setuptools

View File

@ -0,0 +1,8 @@
src/__init__.py,sha256=BRmKeYehopKv4NG_SFa7t6wn248RrPHJivu7DM1R-Rw,48
src/__main__.py,sha256=8TtsnLsaJEM35Y4L8ocrv-qfxusgYpRL2HPyYiabHng,242
my_test_code-0.0.1+2025030514073312.dist-info/METADATA,sha256=nidQMSt6OxDHKdNCQGq1Kv_AmJa1ldwCMHRqPk2TFD8,214
my_test_code-0.0.1+2025030514073312.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
my_test_code-0.0.1+2025030514073312.dist-info/entry_points.txt,sha256=oDWOW9SsBlk4Uejj1ftYPBxfhJ5ZJctb4JOUIG1rc-4,34
my_test_code-0.0.1+2025030514073312.dist-info/top_level.txt,sha256=74rtVfumQlgAPzR5_2CgYN24MB0XARCg0t-gzk6gTrM,4
my_test_code-0.0.1+2025030514073312.dist-info/RECORD,,

View File

@ -0,0 +1,5 @@
Wheel-Version: 1.0
Generator: bdist_wheel (0.42.0)
Root-Is-Purelib: true
Tag: py3-none-any

View File

@ -0,0 +1,2 @@
[group_1]
run = src.__main__:main

View File

@ -0,0 +1,2 @@
__version__ = "0.0.1"
__author__ = "Databricks"

View File

@ -0,0 +1,16 @@
"""
The entry point of the Python Wheel
"""
import sys
def main():
# This method will print the provided arguments
print('Hello from my func')
print('Got arguments:')
print(sys.argv)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,9 @@
title "Test prebuilt wheel:"
trace setmtime.py "2025-03-05 15:07:33.123456789" my_test_code-0.0.1-py3-none-any.whl
trace $CLI selftest patchwhl my_test_code-0.0.1-py3-none-any.whl
mkdir output
cd output
unzip -q ../my_test_code-0.0.1+2025030514073312-py3-none-any.whl
rm ../my_test_code-0.0.1+2025030514073312-py3-none-any.whl

24
cmd/selftest/patchwhl.go Normal file
View File

@ -0,0 +1,24 @@
package selftest
import (
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/patchwheel"
"github.com/spf13/cobra"
)
func newPatchWhl() *cobra.Command {
return &cobra.Command{
Use: "patchwhl",
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
for _, arg := range args {
out, err := patchwheel.PatchWheel(ctx, arg, ".")
if err != nil {
log.Warnf(ctx, "Failed to patch whl: %s: %s", arg, err)
} else {
log.Warnf(ctx, "Patched whl: %s -> %s", arg, out)
}
}
},
}
}

View File

@ -12,5 +12,6 @@ func New() *cobra.Command {
} }
cmd.AddCommand(newPanic()) cmd.AddCommand(newPanic())
cmd.AddCommand(newPatchWhl())
return cmd return cmd
} }

View File

@ -54,7 +54,7 @@ func patchMetadata(r io.Reader, oldVersion, newVersion string) ([]byte, error) {
if foundVersion != oldVersion { if foundVersion != oldVersion {
return nil, fmt.Errorf("Unexpected version in METADATA: %s (expected %s)", strings.TrimSpace(string(line)), oldVersion) return nil, fmt.Errorf("Unexpected version in METADATA: %s (expected %s)", strings.TrimSpace(string(line)), oldVersion)
} }
buf.WriteString(string(versionKey) + newVersion) buf.WriteString(string(versionKey) + " " + newVersion + "\n")
} else { } else {
buf.Write(line) buf.Write(line)
buf.WriteString("\n") buf.WriteString("\n")

View File

@ -1,10 +1,8 @@
package patchwheel package patchwheel
import ( import (
"archive/zip"
"bytes" "bytes"
"context" "context"
"io"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -182,63 +180,6 @@ func TestPatchWheel(t *testing.T) {
} }
} }
func TestPrebuilt(t *testing.T) {
tempDir := t.TempDir()
ctx := context.Background()
// Set fixed modification time for deterministic testing
fixedTime := time.Date(2025, 3, 5, 14, 15, 55, 123456789, time.UTC)
err := os.Chtimes(prebuiltWheel, fixedTime, fixedTime)
require.NoError(t, err)
// With the fixed time, we know exactly what the output filename will be
expectedVersion := "0.0.1+20250305141555.12"
expectedFilename := "my_test_code-" + expectedVersion + "-py3-none-any.whl"
expectedPath := filepath.Join(tempDir, expectedFilename)
outname, err := PatchWheel(ctx, prebuiltWheel, tempDir)
require.NoError(t, err)
require.Equal(t, expectedPath, outname)
_, err = os.Stat(outname)
require.NoError(t, err)
// Verify the contents of the patched wheel
archive, err := zip.OpenReader(outname)
require.NoError(t, err)
defer archive.Close()
// With fixed time, we know the exact dist-info directory name
distInfoPrefix := "my_test_code-" + expectedVersion + ".dist-info/"
// Find METADATA and RECORD files
var metadataContent, recordContent []byte
for _, f := range archive.File {
if f.Name == distInfoPrefix+"METADATA" {
rc, err := f.Open()
require.NoError(t, err)
metadataContent, err = io.ReadAll(rc)
rc.Close()
require.NoError(t, err)
} else if f.Name == distInfoPrefix+"RECORD" {
rc, err := f.Open()
require.NoError(t, err)
recordContent, err = io.ReadAll(rc)
rc.Close()
require.NoError(t, err)
}
}
// Verify METADATA contains the expected version
require.NotNil(t, metadataContent, "METADATA file not found in wheel")
assert.Contains(t, string(metadataContent), "Version: "+expectedVersion)
// Verify RECORD contains entries with the correct dist-info prefix
require.NotNil(t, recordContent, "RECORD file not found in wheel")
assert.Contains(t, string(recordContent), distInfoPrefix+"METADATA")
assert.Contains(t, string(recordContent), distInfoPrefix+"RECORD")
}
func errPatchWheel(t *testing.T, name, out string) { func errPatchWheel(t *testing.T, name, out string) {
ctx := context.Background() ctx := context.Background()
outname, err := PatchWheel(ctx, name, out) outname, err := PatchWheel(ctx, name, out)