|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 Trustify Dependency Analytics Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package io.github.guacsec.trustifyda.providers; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 21 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.Path; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.io.TempDir; |
| 28 | +import org.junitpioneer.jupiter.ClearSystemProperty; |
| 29 | + |
| 30 | +/** |
| 31 | + * Tests for PythonProviderFactory lock file walk-up. Verifies that the factory can find uv.lock in |
| 32 | + * parent directories for workspace member packages. |
| 33 | + */ |
| 34 | +class PythonProviderFactoryLockFileTest { |
| 35 | + |
| 36 | + private static final String PYPROJECT_TOML = |
| 37 | + "[project]\nname = \"my-lib\"\nversion = \"1.0.0\"\ndependencies = []\n"; |
| 38 | + |
| 39 | + /** Lock file in manifest directory (existing behavior — fast path). */ |
| 40 | + @Test |
| 41 | + void testLockFileInManifestDir(@TempDir Path tempDir) throws IOException { |
| 42 | + Files.writeString(tempDir.resolve("pyproject.toml"), PYPROJECT_TOML); |
| 43 | + Files.writeString(tempDir.resolve("uv.lock"), "version = 1"); |
| 44 | + |
| 45 | + var provider = PythonProviderFactory.create(tempDir.resolve("pyproject.toml")); |
| 46 | + assertThat(provider).isInstanceOf(PythonUvProvider.class); |
| 47 | + } |
| 48 | + |
| 49 | + /** No lock file — falls back to pip provider. */ |
| 50 | + @Test |
| 51 | + void testNoLockFileFallsToPip(@TempDir Path tempDir) throws IOException { |
| 52 | + Files.writeString(tempDir.resolve("pyproject.toml"), PYPROJECT_TOML); |
| 53 | + |
| 54 | + var provider = PythonProviderFactory.create(tempDir.resolve("pyproject.toml")); |
| 55 | + assertThat(provider).isInstanceOf(PythonPyprojectProvider.class); |
| 56 | + } |
| 57 | + |
| 58 | + /** Lock file at workspace root, member pyproject.toml in subdirectory. */ |
| 59 | + @Test |
| 60 | + void testLockFileFoundInParentDir(@TempDir Path tempDir) throws IOException { |
| 61 | + // Workspace root with uv workspace config and lock file |
| 62 | + Files.writeString( |
| 63 | + tempDir.resolve("pyproject.toml"), |
| 64 | + "[project]\nname = \"workspace\"\nversion = \"1.0.0\"\n\n" |
| 65 | + + "[tool.uv.workspace]\nmembers = [\"packages/*\"]\n"); |
| 66 | + Files.writeString(tempDir.resolve("uv.lock"), "version = 1"); |
| 67 | + |
| 68 | + // Member package without its own lock file |
| 69 | + Path memberDir = tempDir.resolve("packages/my-lib"); |
| 70 | + Files.createDirectories(memberDir); |
| 71 | + Files.writeString(memberDir.resolve("pyproject.toml"), PYPROJECT_TOML); |
| 72 | + |
| 73 | + var provider = PythonProviderFactory.create(memberDir.resolve("pyproject.toml")); |
| 74 | + assertThat(provider).isInstanceOf(PythonUvProvider.class); |
| 75 | + } |
| 76 | + |
| 77 | + /** TRUSTIFY_DA_WORKSPACE_DIR overrides walk-up. */ |
| 78 | + @Test |
| 79 | + @ClearSystemProperty(key = "TRUSTIFY_DA_WORKSPACE_DIR") |
| 80 | + void testWorkspaceDirOverride(@TempDir Path tempDir) throws IOException { |
| 81 | + // Custom dir with uv lock file |
| 82 | + Path customDir = tempDir.resolve("custom-root"); |
| 83 | + Files.createDirectories(customDir); |
| 84 | + Files.writeString(customDir.resolve("uv.lock"), "version = 1"); |
| 85 | + |
| 86 | + // Member without lock file |
| 87 | + Path memberDir = tempDir.resolve("packages/svc"); |
| 88 | + Files.createDirectories(memberDir); |
| 89 | + Files.writeString(memberDir.resolve("pyproject.toml"), PYPROJECT_TOML); |
| 90 | + |
| 91 | + System.setProperty("TRUSTIFY_DA_WORKSPACE_DIR", customDir.toString()); |
| 92 | + try { |
| 93 | + var provider = PythonProviderFactory.create(memberDir.resolve("pyproject.toml")); |
| 94 | + assertThat(provider).isInstanceOf(PythonUvProvider.class); |
| 95 | + } finally { |
| 96 | + System.clearProperty("TRUSTIFY_DA_WORKSPACE_DIR"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** Walk-up stops at uv workspace root boundary without lock file — falls back to pip. */ |
| 101 | + @Test |
| 102 | + void testStopsAtUvWorkspaceRootBoundary(@TempDir Path tempDir) throws IOException { |
| 103 | + // Workspace root with uv workspace config but NO lock file |
| 104 | + Files.writeString( |
| 105 | + tempDir.resolve("pyproject.toml"), |
| 106 | + "[project]\nname = \"workspace\"\nversion = \"1.0.0\"\n\n" |
| 107 | + + "[tool.uv.workspace]\nmembers = [\"packages/*\"]\n"); |
| 108 | + |
| 109 | + // Member package |
| 110 | + Path memberDir = tempDir.resolve("packages/lib"); |
| 111 | + Files.createDirectories(memberDir); |
| 112 | + Files.writeString(memberDir.resolve("pyproject.toml"), PYPROJECT_TOML); |
| 113 | + |
| 114 | + // Should fall back to pip, not keep walking up |
| 115 | + var provider = PythonProviderFactory.create(memberDir.resolve("pyproject.toml")); |
| 116 | + assertThat(provider).isInstanceOf(PythonPyprojectProvider.class); |
| 117 | + } |
| 118 | + |
| 119 | + /** validateLockFile passes for workspace member when uv.lock is in parent directory. */ |
| 120 | + @Test |
| 121 | + void testValidateLockFilePassesWithLockInParent(@TempDir Path tempDir) throws IOException { |
| 122 | + // Workspace root with uv.lock |
| 123 | + Files.writeString( |
| 124 | + tempDir.resolve("pyproject.toml"), |
| 125 | + "[project]\nname = \"workspace\"\nversion = \"1.0.0\"\n\n" |
| 126 | + + "[tool.uv.workspace]\nmembers = [\"packages/*\"]\n"); |
| 127 | + Files.writeString(tempDir.resolve("uv.lock"), "version = 1"); |
| 128 | + |
| 129 | + // Member package without its own lock file |
| 130 | + Path memberDir = tempDir.resolve("packages/my-lib"); |
| 131 | + Files.createDirectories(memberDir); |
| 132 | + Files.writeString(memberDir.resolve("pyproject.toml"), PYPROJECT_TOML); |
| 133 | + |
| 134 | + var provider = new PythonUvProvider(memberDir.resolve("pyproject.toml")); |
| 135 | + // Ecosystem.getProvider() calls validateLockFile(manifestPath.getParent()) |
| 136 | + assertThatCode(() -> provider.validateLockFile(memberDir)).doesNotThrowAnyException(); |
| 137 | + } |
| 138 | + |
| 139 | + /** validateLockFile throws when uv.lock is not found anywhere. */ |
| 140 | + @Test |
| 141 | + void testValidateLockFileThrowsWhenNotFound(@TempDir Path tempDir) throws IOException { |
| 142 | + Path memberDir = tempDir.resolve("packages/my-lib"); |
| 143 | + Files.createDirectories(memberDir); |
| 144 | + Files.writeString(memberDir.resolve("pyproject.toml"), PYPROJECT_TOML); |
| 145 | + |
| 146 | + var provider = new PythonUvProvider(memberDir.resolve("pyproject.toml")); |
| 147 | + assertThatThrownBy(() -> provider.validateLockFile(memberDir)) |
| 148 | + .isInstanceOf(IllegalStateException.class) |
| 149 | + .hasMessageContaining("uv.lock does not exist"); |
| 150 | + } |
| 151 | +} |
0 commit comments