Skip to content

Commit

Permalink
Simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 30, 2024
1 parent 39afd1b commit 34edf41
Showing 1 changed file with 57 additions and 32 deletions.
89 changes: 57 additions & 32 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1676,18 +1676,49 @@ fn run_isolated_incompatible_python() -> Result<()> {
fn run_compiled_python_file() -> Result<()> {
let context = TestContext::new("3.12");

let pyproject_toml = context.temp_dir.child("pyproject.toml");
pyproject_toml.write_str(indoc! { r#"
[project]
name = "foo"
version = "1.0.0"
requires-python = ">=3.8"
dependencies = ["anyio"]
"#
// Write a non-PEP 723 script.
let test_non_script = context.temp_dir.child("main.py");
test_non_script.write_str(indoc! { r#"
print("Hello, world!")
"#
})?;

// Run a non-PEP 723 script.
uv_snapshot!(context.filters(), context.run().arg("main.py"), @r###"
success: true
exit_code: 0
----- stdout -----
Hello, world!
----- stderr -----
"###);

let compile_output = context
.run()
.arg("python")
.arg("-m")
.arg("compileall")
.arg(test_non_script.path())
.output()?;

assert!(
compile_output.status.success(),
"Failed to compile the python script"
);

// Run the compiled non-PEP 723 script.
let compiled_non_script = context.temp_dir.child("__pycache__/main.cpython-312.pyc");
uv_snapshot!(context.filters(), context.run().arg(compiled_non_script.path()), @r###"
success: true
exit_code: 0
----- stdout -----
Hello, world!
----- stderr -----
"###);

// If the script contains a PEP 723 tag, we should install its requirements.
let test_script = context.temp_dir.child("main.py");
let test_script = context.temp_dir.child("script.py");
test_script.write_str(indoc! { r#"
# /// script
# requires-python = ">=3.11"
Expand All @@ -1699,51 +1730,45 @@ fn run_compiled_python_file() -> Result<()> {
"#
})?;

let test_non_script = context.temp_dir.child("main.py");
test_non_script.write_str(indoc! { r#"
import idna
print("Hello, world!")
"#
})?;

uv_snapshot!(context.filters(), context.run().arg("main.py"), @r###"
uv_snapshot!(context.filters(), context.run().arg("script.py"), @r###"
success: true
exit_code: 0
----- stdout -----
Hello, world!
----- stderr -----
Resolved 6 packages in [TIME]
Prepared 3 packages in [TIME]
Installed 3 packages in [TIME]
+ anyio==4.3.0
+ idna==3.6
+ sniffio==1.3.1
Reading inline script metadata from: script.py
Resolved 1 package in [TIME]
Prepared 1 package in [TIME]
Installed 1 package in [TIME]
+ iniconfig==2.0.0
"###);

let compiled_non_script = context.temp_dir.child("__pycache__/main.cpython-312.pyc");
// Compile the PEP 723 script.
let compile_output = context
.run()
.arg("python")
.arg("-m")
.arg("compileall")
.arg(test_non_script.path())
.arg(test_script.path())
.output()?;

assert!(
compile_output.status.success(),
"Failed to compile the python script"
);

uv_snapshot!(context.filters(), context.run().arg(compiled_non_script.path()), @r###"
success: true
exit_code: 0
// Run the compiled PEP 723 script. This fails, since we can't read the script tag.
let compiled_script = context.temp_dir.child("__pycache__/script.cpython-312.pyc");
uv_snapshot!(context.filters(), context.run().arg(compiled_script.path()), @r###"
success: false
exit_code: 1
----- stdout -----
Hello, world!
----- stderr -----
Resolved 6 packages in [TIME]
Audited 3 packages in [TIME]
Traceback (most recent call last):
File "[TEMP_DIR]/script.py", line 7, in <module>
import iniconfig
ModuleNotFoundError: No module named 'iniconfig'
"###);

Ok(())
Expand Down

0 comments on commit 34edf41

Please sign in to comment.