-
Notifications
You must be signed in to change notification settings - Fork 518
feat: Add the main file for determine package reachability level of Python project #2131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: python-reach
Are you sure you want to change the base?
Changes from all commits
9feba41
388c206
c26429c
413dab2
fddeaf7
c5f2001
a652588
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| 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. | ||||||
| // 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", | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be a typo in the file path for this test case. The directory is named
Suggested change
|
||||||
| expectedResult: nil, | ||||||
| expectError: true, | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| for _, tc := range testCases { | ||||||
| t.Run(tc.name, func(t *testing.T) { | ||||||
| ctx := context.Background() | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you may use |
||||||
| 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') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment.
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.