@@ -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,8 @@ pub struct PythonEnvironment {
2314}
2415
2516impl PythonEnvironment {
26- fn new ( project_path : & Path , venv_path : Option < & str > ) -> Option < Self > {
17+ #[ must_use]
18+ pub fn new ( project_path : & Path , venv_path : Option < & str > ) -> Option < Self > {
2719 if let Some ( path) = venv_path {
2820 let prefix = PathBuf :: from ( path) ;
2921 if let Some ( env) = Self :: from_venv_prefix ( & prefix) {
@@ -703,12 +695,57 @@ mod tests {
703695 }
704696 }
705697
706- // Add tests for the salsa tracked function
707698 mod salsa_integration {
699+ use std:: sync:: Arc ;
700+
701+ use djls_workspace:: FileSystem ;
702+ use djls_workspace:: InMemoryFileSystem ;
703+
708704 use super :: * ;
709- use crate :: db:: ProjectDatabase ;
705+ use crate :: db:: find_python_environment;
706+ use crate :: db:: Db as ProjectDb ;
710707 use crate :: meta:: ProjectMetadata ;
711708
709+ /// Test implementation of ProjectDb for unit tests
710+ #[ salsa:: db]
711+ #[ derive( Clone ) ]
712+ struct TestDatabase {
713+ storage : salsa:: Storage < TestDatabase > ,
714+ metadata : ProjectMetadata ,
715+ fs : Arc < dyn FileSystem > ,
716+ }
717+
718+ impl TestDatabase {
719+ fn new ( metadata : ProjectMetadata ) -> Self {
720+ Self {
721+ storage : salsa:: Storage :: new ( None ) ,
722+ metadata,
723+ fs : Arc :: new ( InMemoryFileSystem :: new ( ) ) ,
724+ }
725+ }
726+ }
727+
728+ #[ salsa:: db]
729+ impl salsa:: Database for TestDatabase { }
730+
731+ #[ salsa:: db]
732+ impl djls_workspace:: Db for TestDatabase {
733+ fn fs ( & self ) -> Arc < dyn FileSystem > {
734+ self . fs . clone ( )
735+ }
736+
737+ fn read_file_content ( & self , path : & std:: path:: Path ) -> std:: io:: Result < String > {
738+ self . fs . read_to_string ( path)
739+ }
740+ }
741+
742+ #[ salsa:: db]
743+ impl ProjectDb for TestDatabase {
744+ fn metadata ( & self ) -> & ProjectMetadata {
745+ & self . metadata
746+ }
747+ }
748+
712749 #[ test]
713750 fn test_find_python_environment_with_salsa_db ( ) {
714751 let project_dir = tempdir ( ) . unwrap ( ) ;
@@ -721,8 +758,8 @@ mod tests {
721758 let metadata =
722759 ProjectMetadata :: new ( project_dir. path ( ) . to_path_buf ( ) , Some ( venv_prefix. clone ( ) ) ) ;
723760
724- // Create a ProjectDatabase with the metadata
725- let db = ProjectDatabase :: new ( metadata) ;
761+ // Create a TestDatabase with the metadata
762+ let db = TestDatabase :: new ( metadata) ;
726763
727764 // Call the tracked function
728765 let env = find_python_environment ( & db) ;
@@ -756,8 +793,8 @@ mod tests {
756793 // Create a metadata instance with project path but no explicit venv path
757794 let metadata = ProjectMetadata :: new ( project_dir. path ( ) . to_path_buf ( ) , None ) ;
758795
759- // Create a ProjectDatabase with the metadata
760- let db = ProjectDatabase :: new ( metadata) ;
796+ // Create a TestDatabase with the metadata
797+ let db = TestDatabase :: new ( metadata) ;
761798
762799 // Mock to ensure VIRTUAL_ENV is not set
763800 let _guard = system:: mock:: MockGuard ;
0 commit comments