| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -92,4 +92,8 @@ jobs: | |||
| 92 | 92 | run: pytest --runtime netfx | |
| 93 | 93 | ||
| 94 | 94 | - name: Python tests run from .NET | |
| 95 | + # For some reason, it won't find pytest on the Windows + 3.10 | ||
| 96 | + # combination, which hints that it does not handle the venv properly in | ||
| 97 | + # this combination. | ||
| 98 | + if: ${{ matrix.os.category != 'windows' || matrix.python != '3.10' }} | ||
| 95 | 99 | run: dotnet test --runtime any-${{ matrix.os.platform }} src/python_tests_runner/ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ | |||
| 9 | 9 | <FullVersion>$([System.IO.File]::ReadAllText("$(MSBuildThisFileDirectory)version.txt").Trim())</FullVersion> | |
| 10 | 10 | <VersionPrefix>$(FullVersion.Split('-', 2)[0])</VersionPrefix> | |
| 11 | 11 | <VersionSuffix Condition="$(FullVersion.Contains('-'))">$(FullVersion.Split('-', 2)[1])</VersionSuffix> | |
| 12 | + <RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot> | ||
| 12 | 13 | </PropertyGroup> | |
| 13 | 14 | <ItemGroup> | |
| 14 | 15 | <PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,4 +23,12 @@ | |||
| 23 | 23 | </PackageReference> | |
| 24 | 24 | </ItemGroup> | |
| 25 | 25 | ||
| 26 | + <Target Name="WriteRepoRootFile" AfterTargets="Build"> | ||
| 27 | + <WriteLinesToFile | ||
| 28 | + File="$(OutputPath)tests_location.txt" | ||
| 29 | + Lines="$(RepositoryRoot)/tests" | ||
| 30 | + Overwrite="true" | ||
| 31 | + /> | ||
| 32 | + </Target> | ||
| 33 | + | ||
| 26 | 34 | </Project> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,22 +8,29 @@ | |||
| 8 | 8 | using NUnit.Framework; | |
| 9 | 9 | ||
| 10 | 10 | using Python.Runtime; | |
| 11 | - using Python.Test; | ||
| 12 | 11 | ||
| 13 | 12 | namespace Python.PythonTestsRunner | |
| 14 | 13 | { | |
| 15 | 14 | public class PythonTestRunner | |
| 16 | 15 | { | |
| 16 | + string OriginalDirectory; | ||
| 17 | + | ||
| 17 | 18 | [OneTimeSetUp] | |
| 18 | 19 | public void SetUp() | |
| 19 | 20 | { | |
| 20 | 21 | PythonEngine.Initialize(); | |
| 22 | + OriginalDirectory = Environment.CurrentDirectory; | ||
| 23 | + | ||
| 24 | + var codeDir = File.ReadAllText("tests_location.txt").Trim(); | ||
| 25 | + TestContext.Progress.WriteLine($"Changing working directory to {codeDir}"); | ||
| 26 | + Environment.CurrentDirectory = codeDir; | ||
| 21 | 27 | } | |
| 22 | 28 | ||
| 23 | 29 | [OneTimeTearDown] | |
| 24 | 30 | public void Dispose() | |
| 25 | 31 | { | |
| 26 | 32 | PythonEngine.Shutdown(); | |
| 33 | + Environment.CurrentDirectory = OriginalDirectory; | ||
| 27 | 34 | } | |
| 28 | 35 | ||
| 29 | 36 | /// <summary> | |
@@ -46,39 +53,15 @@ static IEnumerable<string[]> PythonTestCases() | |||
| 46 | 53 | [TestCaseSource(nameof(PythonTestCases))] | |
| 47 | 54 | public void RunPythonTest(string testFile, string testName) | |
| 48 | 55 | { | |
| 49 | - // Find the tests directory | ||
| 50 | - string folder = typeof(PythonTestRunner).Assembly.Location; | ||
| 51 | - while (Path.GetFileName(folder) != "src") | ||
| 56 | + using dynamic pytest = Py.Import("pytest"); | ||
| 57 | + | ||
| 58 | + using var args = new PyList(); | ||
| 59 | + args.Append(new PyString($"{testFile}.py::{testName}")); | ||
| 60 | + int res = pytest.main(args); | ||
| 61 | + if (res != 0) | ||
| 52 | 62 | { | |
| 53 | - folder = Path.GetDirectoryName(folder); | ||
| 63 | + Assert.Fail($"Python test {testFile}.{testName} failed"); | ||
| 54 | 64 | } | |
| 55 | - folder = Path.Combine(folder, "..", "tests"); | ||
| 56 | - string path = Path.Combine(folder, testFile + ".py"); | ||
| 57 | - if (!File.Exists(path)) throw new FileNotFoundException("Cannot find test file", path); | ||
| 58 | - | ||
| 59 | - // We could use 'import' below, but importlib gives more helpful error messages than 'import' | ||
| 60 | - // https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly | ||
| 61 | - // Because the Python tests sometimes have relative imports, the module name must be inside the tests package | ||
| 62 | - PythonEngine.Exec($@" | ||
| 63 | - import sys | ||
| 64 | - import os | ||
| 65 | - sys.path.append(os.path.dirname(r'{folder}')) | ||
| 66 | - sys.path.append(os.path.join(r'{folder}', 'fixtures')) | ||
| 67 | - import clr | ||
| 68 | - clr.AddReference('Python.Test') | ||
| 69 | - import tests | ||
| 70 | - module_name = 'tests.{testFile}' | ||
| 71 | - file_path = r'{path}' | ||
| 72 | - import importlib.util | ||
| 73 | - spec = importlib.util.spec_from_file_location(module_name, file_path) | ||
| 74 | - module = importlib.util.module_from_spec(spec) | ||
| 75 | - sys.modules[module_name] = module | ||
| 76 | - try: | ||
| 77 | - spec.loader.exec_module(module) | ||
| 78 | - except ImportError as error: | ||
| 79 | - raise ImportError(str(error) + ' when sys.path=' + os.pathsep.join(sys.path)) | ||
| 80 | - module.{testName}() | ||
| 81 | - "); | ||
| 82 | 65 | } | |
| 83 | 66 | } | |
| 84 | 67 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -161,7 +161,7 @@ private static Dictionary<string, string> TryParse(string venvCfg) | |||
| 161 | 161 | ||
| 162 | 162 | private static string ProgramNameFromPath(string path) | |
| 163 | 163 | { | |
| 164 | - if (Runtime.IsWindows) | ||
| 164 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| 165 | 165 | { | |
| 166 | 166 | return Path.Combine(path, "Scripts", "python.exe"); | |
| 167 | 167 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments