Skip to content

Commit

Permalink
Modify default test behavior to run in the project directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Nov 11, 2024
1 parent 2622c12 commit 12912b6
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Options
| | [`CIBW_DEPENDENCY_VERSIONS`](https://cibuildwheel.pypa.io/en/stable/options/#dependency-versions) | Specify how cibuildwheel controls the versions of the tools it uses |
| **Testing** | [`CIBW_TEST_COMMAND`](https://cibuildwheel.pypa.io/en/stable/options/#test-command) | Execute a shell command to test each built wheel |
| | [`CIBW_BEFORE_TEST`](https://cibuildwheel.pypa.io/en/stable/options/#before-test) | Execute a shell command before testing each wheel |
| | [`CIBW_TEST_SOURCES`](https://cibuildwheel.pypa.io/en/stable/options/#test-sources) | Files and folders from the source tree that must be copied into the test environment before running the tests. |
| | [`CIBW_TEST_REQUIRES`](https://cibuildwheel.pypa.io/en/stable/options/#test-requires) | Install Python dependencies before running the tests |
| | [`CIBW_TEST_EXTRAS`](https://cibuildwheel.pypa.io/en/stable/options/#test-extras) | Install your wheel for testing using extras_require |
| | [`CIBW_TEST_SKIP`](https://cibuildwheel.pypa.io/en/stable/options/#test-skip) | Skip running tests on some builds |
Expand Down
9 changes: 5 additions & 4 deletions cibuildwheel/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
prepare_command,
read_python_configs,
split_config_settings,
test_fail_cwd_file,
unwrap,
)

Expand Down Expand Up @@ -402,17 +401,19 @@ def build_in_container(
package=container_package_dir,
wheel=wheel_to_test,
)
test_cwd = testing_temp_dir / "test_cwd"
container.call(["mkdir", "-p", test_cwd])

if build_options.test_sources:
test_cwd = testing_temp_dir / "test_cwd"
container.call(["mkdir", "-p", test_cwd])
copy_test_sources(
build_options.test_sources,
build_options.package_dir,
test_cwd,
copy_into=container.copy_into,
)
else:
container.copy_into(test_fail_cwd_file, test_cwd / "test_fail.py")
# There are no test sources. Run the tests in the project directory.
test_cwd = PurePosixPath(container_project_path)

container.call(["sh", "-c", test_command_prepared], cwd=test_cwd, env=virtualenv_env)

Expand Down
9 changes: 4 additions & 5 deletions cibuildwheel/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
read_python_configs,
shell,
split_config_settings,
test_fail_cwd_file,
unwrap,
virtualenv,
)
Expand Down Expand Up @@ -737,17 +736,17 @@ def build(options: Options, tmp_path: Path) -> None:
wheel=repaired_wheel,
)

test_cwd = identifier_tmp_dir / "test_cwd"
test_cwd.mkdir(exist_ok=True)
if build_options.test_sources:
test_cwd = identifier_tmp_dir / "test_cwd"
test_cwd.mkdir(exist_ok=True)
copy_test_sources(
build_options.test_sources,
build_options.package_dir,
test_cwd,
)
else:
# There are no test sources. Copy the test safety file.
(test_cwd / "test_fail.py").write_text(test_fail_cwd_file.read_text())
# There are no test sources. Run the tests in the project directory.
test_cwd = Path(".").resolve()

shell_with_arch(test_command_prepared, cwd=test_cwd, env=virtualenv_env)

Expand Down
9 changes: 4 additions & 5 deletions cibuildwheel/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
read_python_configs,
shell,
split_config_settings,
test_fail_cwd_file,
virtualenv,
)

Expand Down Expand Up @@ -380,17 +379,17 @@ def build(options: Options, tmp_path: Path) -> None:
package=build_options.package_dir.resolve(),
)

test_cwd = identifier_tmp_dir / "test_cwd"
test_cwd.mkdir(exist_ok=True)
if build_options.test_sources:
test_cwd = identifier_tmp_dir / "test_cwd"
test_cwd.mkdir(exist_ok=True)
copy_test_sources(
build_options.test_sources,
build_options.package_dir,
test_cwd,
)
else:
# There are no test sources. Copy the test safety file.
(test_cwd / "test_fail.py").write_text(test_fail_cwd_file.read_text())
# There are no test sources. Run the tests in the project directory.
test_cwd = Path(".").resolve()

shell(test_command_prepared, cwd=test_cwd, env=virtualenv_env)

Expand Down
19 changes: 0 additions & 19 deletions cibuildwheel/resources/testing_temp_dir_file.py

This file was deleted.

2 changes: 0 additions & 2 deletions cibuildwheel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@

free_thread_enable_313: Final[Path] = resources_dir / "free-threaded-enable-313.xml"

test_fail_cwd_file: Final[Path] = resources_dir / "testing_temp_dir_file.py"


MANYLINUX_ARCHS: Final[tuple[str, ...]] = (
"x86_64",
Expand Down
9 changes: 4 additions & 5 deletions cibuildwheel/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
read_python_configs,
shell,
split_config_settings,
test_fail_cwd_file,
unwrap,
virtualenv,
)
Expand Down Expand Up @@ -573,17 +572,17 @@ def build(options: Options, tmp_path: Path) -> None:
package=options.globals.package_dir.resolve(),
wheel=repaired_wheel,
)
test_cwd = identifier_tmp_dir / "test_cwd"
test_cwd.mkdir()
if build_options.test_sources:
test_cwd = identifier_tmp_dir / "test_cwd"
test_cwd.mkdir()
copy_test_sources(
build_options.test_sources,
build_options.package_dir,
test_cwd,
)
else:
# There are no test sources. Copy the test safety file.
(test_cwd / "test_fail.py").write_text(test_fail_cwd_file.read_text())
# There are no test sources. Run the tests in the project directory.
test_cwd = Path(".").resolve()

shell(test_command_prepared, cwd=test_cwd, env=virtualenv_env)

Expand Down
18 changes: 9 additions & 9 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1413,20 +1413,18 @@ Shell command to run tests after the build. The wheel will be installed
automatically and available for import from the tests. If this variable is not
set, your wheel will not be installed after building.

To ensure the wheel is imported by your tests (instead of your source copy),
**tests are not run from your project directory** - they are run from a
temporary folder generated by cibuildwheel. To reference test sources, you can
use the placeholders `{project}` and `{package}` in your test command to pass in
the location of your test code:
By default, tests are executed from from your project directory. When specifying
`CIBW_TEST_COMMAND`, you can use the placeholders `{project}` and `{package}` to
pass in the location of your test code:

- `{project}` is an absolute path to the project root - the working directory
where cibuildwheel was called.
- `{package}` is the path to the package being built - the `package_dir`
argument supplied to cibuildwheel on the command line.

Alternatively, you can use the `CIBW_TEST_SOURCES` setting to pass in the list
of files and folders to copy into the temporary folder. This should *not*
include any code that is included as part of your wheel.
Alternatively, you can use the [`CIBW_TEST_SOURCES`](#test-sources) setting to
create a temporary folder populated with a specific subset of project files to
run your test suite.

The command is run in a shell, so you can write things like `cmd1 && cmd2`.

Expand Down Expand Up @@ -1546,7 +1544,9 @@ Platform-specific environment variables are also available:<br/>
> Files and folders from the source tree that must be copied into the test environment before running the tests.
A space-separated list of files and folders, relative to the root of the
project, required for running the tests.
project, required for running the tests. If specified, these files and folders
will be copied into a temporary folder, and that temporary folder will be used
as the working directory for running the test suite.

Platform-specific environment variables are also available:<br/>
`CIBW_TEST_SOURCES_MACOS` | `CIBW_TEST_SOURCES_WINDOWS` | `CIBW_TEST_SOURCES_LINUX` | `CIBW_TEST_SOURCES_PYODIDE`
Expand Down

0 comments on commit 12912b6

Please sign in to comment.