11from __future__ import annotations
22
3- from typing import Mapping
43from unittest import mock
54
65import pytest
76
87import pre_commit .constants as C
98from pre_commit import parse_shebang
109from pre_commit .languages import rust
11- from pre_commit .prefix import Prefix
12- from pre_commit . util import cmd_output
10+ from pre_commit .store import _make_local_repo
11+ from testing . language_helpers import run_language
1312
1413ACTUAL_GET_DEFAULT_VERSION = rust .get_default_version .__wrapped__
1514
@@ -30,64 +29,78 @@ def test_uses_default_when_rust_is_not_available(cmd_output_b_mck):
3029 assert ACTUAL_GET_DEFAULT_VERSION () == C .DEFAULT
3130
3231
33- @pytest .mark .parametrize ('language_version' , (C .DEFAULT , '1.56.0' ))
34- def test_installs_with_bootstrapped_rustup (tmpdir , language_version ):
35- tmpdir .join ('src' , 'main.rs' ).ensure ().write (
32+ def _make_hello_world (tmp_path ):
33+ src_dir = tmp_path .joinpath ('src' )
34+ src_dir .mkdir ()
35+ src_dir .joinpath ('main.rs' ).write_text (
3636 'fn main() {\n '
3737 ' println!("Hello, world!");\n '
3838 '}\n ' ,
3939 )
40- tmpdir . join ('Cargo.toml' ).ensure (). write (
40+ tmp_path . joinpath ('Cargo.toml' ).write_text (
4141 '[package]\n '
4242 'name = "hello_world"\n '
4343 'version = "0.1.0"\n '
4444 'edition = "2021"\n ' ,
4545 )
46- prefix = Prefix (str (tmpdir ))
4746
48- find_executable_exes = []
4947
50- original_find_executable = parse_shebang .find_executable
48+ def test_installs_rust_missing_rustup (tmp_path ):
49+ _make_hello_world (tmp_path )
5150
52- def mocked_find_executable (
53- exe : str , * , env : Mapping [str , str ] | None = None ,
54- ) -> str | None :
55- """
56- Return `None` the first time `find_executable` is called to ensure
57- that the bootstrapping code is executed, then just let the function
58- work as normal.
51+ # pretend like `rustup` doesn't exist so it gets bootstrapped
52+ calls = []
53+ orig = parse_shebang .find_executable
5954
60- Also log the arguments to ensure that everything works as expected.
61- """
62- find_executable_exes . append ( exe )
63- if len ( find_executable_exes ) == 1 :
55+ def mck ( exe , env = None ):
56+ calls . append ( exe )
57+ if len ( calls ) == 1 :
58+ assert exe == 'rustup'
6459 return None
65- return original_find_executable (exe , env = env )
60+ return orig (exe , env = env )
6661
67- with mock .patch .object (parse_shebang , 'find_executable' ) as find_exe_mck :
68- find_exe_mck . side_effect = mocked_find_executable
69- rust . install_environment ( prefix , language_version , ())
70- assert find_executable_exes == [ 'rustup' , 'rustup' , 'cargo' ]
62+ with mock .patch .object (parse_shebang , 'find_executable' , side_effect = mck ) :
63+ ret = run_language ( tmp_path , rust , 'hello_world' , version = '1.56.0' )
64+ assert calls == [ 'rustup' , 'rustup' , 'cargo' , 'hello_world' ]
65+ assert ret == ( 0 , b'Hello, world! \n ' )
7166
72- with rust .in_env (prefix , language_version ):
73- assert cmd_output ('hello_world' )[1 ] == 'Hello, world!\n '
7467
68+ @pytest .mark .parametrize ('version' , (C .DEFAULT , '1.56.0' ))
69+ def test_language_version_with_rustup (tmp_path , version ):
70+ assert parse_shebang .find_executable ('rustup' ) is not None
7571
76- def test_installs_with_existing_rustup (tmpdir ):
77- tmpdir .join ('src' , 'main.rs' ).ensure ().write (
78- 'fn main() {\n '
79- ' println!("Hello, world!");\n '
80- '}\n ' ,
81- )
82- tmpdir .join ('Cargo.toml' ).ensure ().write (
83- '[package]\n '
84- 'name = "hello_world"\n '
85- 'version = "0.1.0"\n '
86- 'edition = "2021"\n ' ,
72+ _make_hello_world (tmp_path )
73+
74+ ret = run_language (tmp_path , rust , 'hello_world' , version = version )
75+ assert ret == (0 , b'Hello, world!\n ' )
76+
77+
78+ @pytest .mark .parametrize ('dep' , ('cli:shellharden:4.2.0' , 'cli:shellharden' ))
79+ def test_rust_cli_additional_dependencies (tmp_path , dep ):
80+ _make_local_repo (str (tmp_path ))
81+
82+ t_sh = tmp_path .joinpath ('t.sh' )
83+ t_sh .write_text ('echo $hi\n ' )
84+
85+ assert rust .get_default_version () == 'system'
86+ ret = run_language (
87+ tmp_path ,
88+ rust ,
89+ 'shellharden --transform' ,
90+ deps = (dep ,),
91+ args = (str (t_sh ),),
8792 )
88- prefix = Prefix ( str ( tmpdir ) )
93+ assert ret == ( 0 , b'echo "$hi" \n ' )
8994
90- assert parse_shebang .find_executable ('rustup' ) is not None
91- rust .install_environment (prefix , '1.56.0' , ())
92- with rust .in_env (prefix , '1.56.0' ):
93- assert cmd_output ('hello_world' )[1 ] == 'Hello, world!\n '
95+
96+ def test_run_lib_additional_dependencies (tmp_path ):
97+ _make_hello_world (tmp_path )
98+
99+ deps = ('shellharden:4.2.0' , 'git-version' )
100+ ret = run_language (tmp_path , rust , 'hello_world' , deps = deps )
101+ assert ret == (0 , b'Hello, world!\n ' )
102+
103+ bin_dir = tmp_path .joinpath ('rustenv-system' , 'bin' )
104+ assert bin_dir .is_dir ()
105+ assert not bin_dir .joinpath ('shellharden' ).exists ()
106+ assert not bin_dir .joinpath ('shellharden.exe' ).exists ()
0 commit comments