@@ -4,17 +4,8 @@ use std::path::PathBuf;
44
55use pyo3:: prelude:: * ;
66
7- use crate :: db:: Db ;
87use crate :: system;
98
10- #[ salsa:: tracked]
11- pub fn find_python_environment ( db : & dyn Db ) -> Option < PythonEnvironment > {
12- let project_path = db. metadata ( ) . root ( ) . as_path ( ) ;
13- let venv_path = db. metadata ( ) . venv ( ) . and_then ( |p| p. to_str ( ) ) ;
14-
15- PythonEnvironment :: new ( project_path, venv_path)
16- }
17-
189#[ derive( Clone , Debug , PartialEq ) ]
1910pub struct PythonEnvironment {
2011 python_path : PathBuf ,
@@ -23,7 +14,7 @@ pub struct PythonEnvironment {
2314}
2415
2516impl PythonEnvironment {
26- fn new ( project_path : & Path , venv_path : Option < & str > ) -> Option < Self > {
17+ pub fn new ( project_path : & Path , venv_path : Option < & str > ) -> Option < Self > {
2718 if let Some ( path) = venv_path {
2819 let prefix = PathBuf :: from ( path) ;
2920 if let Some ( env) = Self :: from_venv_prefix ( & prefix) {
@@ -703,12 +694,57 @@ mod tests {
703694 }
704695 }
705696
706- // Add tests for the salsa tracked function
707697 mod salsa_integration {
698+ use std:: sync:: Arc ;
699+
700+ use djls_workspace:: FileSystem ;
701+ use djls_workspace:: InMemoryFileSystem ;
702+
708703 use super :: * ;
709- use crate :: db:: ProjectDatabase ;
704+ use crate :: db:: find_python_environment;
705+ use crate :: db:: Db as ProjectDb ;
710706 use crate :: meta:: ProjectMetadata ;
711707
708+ /// Test implementation of ProjectDb for unit tests
709+ #[ salsa:: db]
710+ #[ derive( Clone ) ]
711+ struct TestDatabase {
712+ storage : salsa:: Storage < TestDatabase > ,
713+ metadata : ProjectMetadata ,
714+ fs : Arc < dyn FileSystem > ,
715+ }
716+
717+ impl TestDatabase {
718+ fn new ( metadata : ProjectMetadata ) -> Self {
719+ Self {
720+ storage : salsa:: Storage :: new ( None ) ,
721+ metadata,
722+ fs : Arc :: new ( InMemoryFileSystem :: new ( ) ) ,
723+ }
724+ }
725+ }
726+
727+ #[ salsa:: db]
728+ impl salsa:: Database for TestDatabase { }
729+
730+ #[ salsa:: db]
731+ impl djls_workspace:: Db for TestDatabase {
732+ fn fs ( & self ) -> Arc < dyn FileSystem > {
733+ self . fs . clone ( )
734+ }
735+
736+ fn read_file_content ( & self , path : & std:: path:: Path ) -> std:: io:: Result < String > {
737+ self . fs . read_to_string ( path)
738+ }
739+ }
740+
741+ #[ salsa:: db]
742+ impl ProjectDb for TestDatabase {
743+ fn metadata ( & self ) -> & ProjectMetadata {
744+ & self . metadata
745+ }
746+ }
747+
712748 #[ test]
713749 fn test_find_python_environment_with_salsa_db ( ) {
714750 let project_dir = tempdir ( ) . unwrap ( ) ;
@@ -721,8 +757,8 @@ mod tests {
721757 let metadata =
722758 ProjectMetadata :: new ( project_dir. path ( ) . to_path_buf ( ) , Some ( venv_prefix. clone ( ) ) ) ;
723759
724- // Create a ProjectDatabase with the metadata
725- let db = ProjectDatabase :: new ( metadata) ;
760+ // Create a TestDatabase with the metadata
761+ let db = TestDatabase :: new ( metadata) ;
726762
727763 // Call the tracked function
728764 let env = find_python_environment ( & db) ;
@@ -756,8 +792,8 @@ mod tests {
756792 // Create a metadata instance with project path but no explicit venv path
757793 let metadata = ProjectMetadata :: new ( project_dir. path ( ) . to_path_buf ( ) , None ) ;
758794
759- // Create a ProjectDatabase with the metadata
760- let db = ProjectDatabase :: new ( metadata) ;
795+ // Create a TestDatabase with the metadata
796+ let db = TestDatabase :: new ( metadata) ;
761797
762798 // Mock to ensure VIRTUAL_ENV is not set
763799 let _guard = system:: mock:: MockGuard ;
0 commit comments