Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
415 changes: 415 additions & 0 deletions experimental/pythonreach/example/poetry.lock

Large diffs are not rendered by default.

415 changes: 415 additions & 0 deletions experimental/pythonreach/example/test.py

Large diffs are not rendered by default.

685 changes: 685 additions & 0 deletions experimental/pythonreach/main.go

Large diffs are not rendered by default.

138 changes: 138 additions & 0 deletions experimental/pythonreach/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package main

import (
"context"
"path/filepath"
"reflect"
"sort"
"testing"
)

// A simple sort utility for comparing slices of LibraryInfo
func sortLibraries(libs []*LibraryInfo) {
sort.Slice(libs, func(i, j int) bool {
return libs[i].Name < libs[j].Name
})
}

func TestFindMainEntryPoint(t *testing.T) {
// Define test cases
testCases := []struct {
name string
directoryPath string
expectedPaths []string
expectError bool
}{
{
name: "Happy Path - Single File",
directoryPath: "./testdata/pythonfilewithentrypoint",
expectedPaths: []string{"testdata/pythonfilewithentrypoint/main.py"},
expectError: false,
},
{
name: "Multiple Files with One Entry Point",
directoryPath: "./testdata/multifileswithentrypoint",
expectedPaths: []string{"testdata/multifileswithentrypoint/main.py"},
expectError: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualPaths, err := findMainEntryPoint(tc.directoryPath)
if tc.expectError {
if err == nil {
t.Errorf("Expected an error, but got none")
}
} else {
if err != nil {
t.Errorf("Did not expect an error, but got: %v", err)
}
}

// To compare slices, we need a canonical order.
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume the main entry point will be a single string so we probably don't need to sort it.

// The expected paths also need to be joined with the temp directory path.
expectedFullPaths := []string{}
for _, path := range tc.expectedPaths {
absPath, err := filepath.Abs(path)
if err != nil {
t.Errorf("Failed to get absolute path for %s: %v", path, err)
}
expectedFullPaths = append(expectedFullPaths, absPath)
}

sort.Strings(actualPaths)
sort.Strings(expectedFullPaths)

if !reflect.DeepEqual(actualPaths, expectedFullPaths) {
t.Errorf("Expected paths %v, but got %v", expectedFullPaths, actualPaths)
}
})
}

t.Run("Non-existent Directory", func(t *testing.T) {
_, err := findMainEntryPoint("path/that/does/not/exist")
if err == nil {
t.Errorf("Expected an error for a non-existent directory, but got none")
}
})
}

func TestParsePoetryLibrary(t *testing.T) {
testCases := []struct {
name string
fpathInTestDir string // The fpath to pass to the function.
expectedResult []*LibraryInfo
expectError bool
}{
{
name: "Happy Path - Valid poetry.lock",
fpathInTestDir: "./testdata/pythonfilewithentrypoint/poetry.lock",
expectedResult: []*LibraryInfo{
{Name: "numpy", Version: "1.26.4"},
{Name: "pandas", Version: "2.2.2"},
},
expectError: false,
},
{
name: "File Not Found - No poetry.lock",
fpathInTestDir: "./testdata/test/poetry.lock",
expectedResult: nil,
expectError: true,
},
{
name: "Malformed poetry.lock - Parser error",
fpathInTestDir: "./testdata/tmultifileswithentrypoint/poetry.lock",
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There seems to be a typo in the file path for this test case. The directory is named multifileswithentrypoint, but the path used is ./testdata/tmultifileswithentrypoint/poetry.lock (with an extra 't'). This will cause the test to pass because os.Open will fail to find the file, which matches expectError: true, but it's not testing what's intended (parsing a malformed file).

Suggested change
fpathInTestDir: "./testdata/tmultifileswithentrypoint/poetry.lock",
fpathInTestDir: "./testdata/multifileswithentrypoint/poetry.lock",

expectedResult: nil,
expectError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
Copy link
Contributor

Choose a reason for hiding this comment

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

you may use t.Context() for context in testing

absDir, err := filepath.Abs(tc.fpathInTestDir)
if err != nil {
t.Errorf("Failed to get absolute path for %s: %v", tc.fpathInTestDir, err)
}
actualResult, err := parsePoetryLock(ctx, absDir)
if tc.expectError {
if err == nil {
t.Errorf("Expected an error, but got nil")
}
} else {
if err != nil {
t.Errorf("Did not expect an error, but got: %v", err)
}
}

// Sort both slices to ensure a consistent order for comparison.
sortLibraries(actualResult)
sortLibraries(tc.expectedResult)

if !reflect.DeepEqual(actualResult, tc.expectedResult) {
t.Errorf("Expected result %v, but got %v", tc.expectedResult, actualResult)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print('hello')
Copy link
Contributor

Choose a reason for hiding this comment

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

let's add newline to end of these test files if that won't bring any trouble

if __name__ == '__main__':
pass

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

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

is there any particular reason that we want to keep this shell script?

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def helper():
return 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print('hello')
if __name__ == '__main__':
pass

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.