@@ -229,4 +229,120 @@ mod tests {
229229
230230 assert ! ( locator. try_from( & env) . is_none( ) ) ;
231231 }
232+
233+ // ── is_pixi_env edge cases ────────────────────────────────────
234+
235+ #[ test]
236+ fn is_pixi_env_rejects_conda_meta_without_pixi_marker ( ) {
237+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
238+ let prefix = temp_dir. path ( ) . join ( "conda-env" ) ;
239+ fs:: create_dir_all ( prefix. join ( "conda-meta" ) ) . unwrap ( ) ;
240+ // conda-meta exists but no pixi marker file
241+ assert ! ( !is_pixi_env( & prefix) ) ;
242+ }
243+
244+ #[ test]
245+ fn is_pixi_env_rejects_path_without_conda_meta ( ) {
246+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
247+ let prefix = temp_dir. path ( ) . join ( "plain-dir" ) ;
248+ fs:: create_dir_all ( & prefix) . unwrap ( ) ;
249+ assert ! ( !is_pixi_env( & prefix) ) ;
250+ }
251+
252+ // ── get_pixi_prefix edge cases ────────────────────────────────
253+
254+ #[ test]
255+ fn get_pixi_prefix_returns_none_for_non_bin_parent_without_pixi ( ) {
256+ // Executable is in a directory that is neither a pixi env itself
257+ // nor named "bin"/"Scripts", so prefix cannot be derived.
258+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
259+ let lib_dir = temp_dir. path ( ) . join ( "lib" ) ;
260+ fs:: create_dir_all ( & lib_dir) . unwrap ( ) ;
261+ let executable = lib_dir. join ( "python" ) ;
262+ fs:: write ( & executable, b"" ) . unwrap ( ) ;
263+
264+ let env = PythonEnv :: new ( executable, None , None ) ;
265+ assert ! ( get_pixi_prefix( & env) . is_none( ) ) ;
266+ }
267+
268+ #[ test]
269+ fn get_pixi_prefix_returns_none_when_bin_parent_is_not_pixi ( ) {
270+ // Executable is in bin/ but the parent of bin/ is not a pixi env.
271+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
272+ let prefix = temp_dir. path ( ) . join ( "not-pixi" ) ;
273+ let bin_dir = prefix. join ( "bin" ) ;
274+ fs:: create_dir_all ( & bin_dir) . unwrap ( ) ;
275+ let executable = bin_dir. join ( "python" ) ;
276+ fs:: write ( & executable, b"" ) . unwrap ( ) ;
277+
278+ let env = PythonEnv :: new ( executable, None , None ) ;
279+ assert ! ( get_pixi_prefix( & env) . is_none( ) ) ;
280+ }
281+
282+ #[ test]
283+ fn get_pixi_prefix_prefers_explicit_prefix_over_executable_derivation ( ) {
284+ // When env.prefix is set, it should be returned directly
285+ // even if the executable is in a different location.
286+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
287+ let explicit_prefix = temp_dir. path ( ) . join ( "explicit" ) ;
288+ fs:: create_dir_all ( & explicit_prefix) . unwrap ( ) ;
289+
290+ let env = PythonEnv :: new (
291+ temp_dir. path ( ) . join ( "somewhere-else" ) . join ( "python" ) ,
292+ Some ( explicit_prefix. clone ( ) ) ,
293+ None ,
294+ ) ;
295+
296+ let result = get_pixi_prefix ( & env) ;
297+ // Compare file names rather than full paths to avoid Windows 8.3 short path issues
298+ assert_eq ! (
299+ result. as_ref( ) . unwrap( ) . file_name( ) ,
300+ explicit_prefix. file_name( )
301+ ) ;
302+ }
303+
304+ // ── try_from field validation ─────────────────────────────────
305+
306+ #[ test]
307+ fn try_from_populates_name_from_prefix_directory ( ) {
308+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
309+ // Use a custom directory name to verify name extraction
310+ let prefix = temp_dir. path ( ) . join ( "my-custom-env" ) ;
311+ fs:: create_dir_all ( prefix. join ( "conda-meta" ) ) . unwrap ( ) ;
312+ fs:: write ( prefix. join ( "conda-meta" ) . join ( "pixi" ) , b"" ) . unwrap ( ) ;
313+ let bin_dir = prefix. join ( if cfg ! ( windows) { "Scripts" } else { "bin" } ) ;
314+ fs:: create_dir_all ( & bin_dir) . unwrap ( ) ;
315+ let executable = bin_dir. join ( if cfg ! ( windows) {
316+ "python.exe"
317+ } else {
318+ "python"
319+ } ) ;
320+ fs:: write ( & executable, b"" ) . unwrap ( ) ;
321+
322+ let locator = Pixi :: new ( ) ;
323+ let env = PythonEnv :: new ( executable, Some ( prefix) , None ) ;
324+
325+ let pixi_env = locator. try_from ( & env) . unwrap ( ) ;
326+ assert_eq ! ( pixi_env. name, Some ( "my-custom-env" . to_string( ) ) ) ;
327+ }
328+
329+ #[ test]
330+ fn try_from_rejects_when_explicit_prefix_is_not_pixi ( ) {
331+ // env.prefix is set but it's not a pixi env (no conda-meta/pixi)
332+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
333+ let prefix = temp_dir. path ( ) . join ( "not-pixi" ) ;
334+ fs:: create_dir_all ( & prefix) . unwrap ( ) ;
335+
336+ let executable = prefix. join ( if cfg ! ( windows) {
337+ "python.exe"
338+ } else {
339+ "python"
340+ } ) ;
341+ fs:: write ( & executable, b"" ) . unwrap ( ) ;
342+
343+ let locator = Pixi :: new ( ) ;
344+ let env = PythonEnv :: new ( executable, Some ( prefix) , None ) ;
345+
346+ assert ! ( locator. try_from( & env) . is_none( ) ) ;
347+ }
232348}
0 commit comments