11use std:: fs;
22use std:: io:: Write ;
3- use std:: path:: PathBuf ;
3+ use std:: path:: { PathBuf } ;
44
55pub struct ProjectGenerator {
66 project_name : String ,
@@ -35,17 +35,14 @@ impl ProjectGenerator {
3535
3636 // Create standard directories
3737 self . create_directories ( ) ?;
38-
38+
3939 // Create project.toml
4040 self . create_project_toml ( ) ?;
41-
41+
4242 // Create basic example file
4343 self . create_example_file ( ) ?;
4444
45- println ! (
46- "Successfully created new AIScript project: {}" ,
47- self . project_name
48- ) ;
45+ println ! ( "Successfully created new AIScript project: {}" , self . project_name) ;
4946 println ! ( "Project structure:" ) ;
5047 println ! ( "{}" , self . display_project_structure( ) ) ;
5148 println ! ( "" ) ;
@@ -56,21 +53,21 @@ impl ProjectGenerator {
5653
5754 fn create_directories ( & self ) -> Result < ( ) , String > {
5855 let dirs = vec ! [ "lib" , "routes" ] ;
59-
56+
6057 for dir in dirs {
6158 let dir_path = self . project_path . join ( dir) ;
6259 fs:: create_dir_all ( & dir_path) . map_err ( |e| {
6360 format ! ( "Failed to create directory '{}': {}" , dir_path. display( ) , e)
6461 } ) ?;
6562 }
66-
63+
6764 Ok ( ( ) )
6865 }
6966
7067 fn create_project_toml ( & self ) -> Result < ( ) , String > {
7168 let toml_path = self . project_path . join ( "project.toml" ) ;
7269 let username = whoami:: username ( ) ;
73-
70+
7471 let toml_content = format ! (
7572 r#"[project]
7673name = "{}"
@@ -89,20 +86,22 @@ path = "/docs"
8986"# ,
9087 self . project_name, username
9188 ) ;
92-
93- let mut file = fs:: File :: create ( & toml_path)
94- . map_err ( |e| format ! ( "Failed to create project.toml: {}" , e) ) ?;
95-
96- file. write_all ( toml_content. as_bytes ( ) )
97- . map_err ( |e| format ! ( "Failed to write to project.toml: {}" , e) ) ?;
98-
89+
90+ let mut file = fs:: File :: create ( & toml_path) . map_err ( |e| {
91+ format ! ( "Failed to create project.toml: {}" , e)
92+ } ) ?;
93+
94+ file. write_all ( toml_content. as_bytes ( ) ) . map_err ( |e| {
95+ format ! ( "Failed to write to project.toml: {}" , e)
96+ } ) ?;
97+
9998 Ok ( ( ) )
10099 }
101100
102101 fn create_example_file ( & self ) -> Result < ( ) , String > {
103102 let routes_dir = self . project_path . join ( "routes" ) ;
104103 let example_path = routes_dir. join ( "index.ai" ) ;
105-
104+
106105 let example_content = r#"// Example AIScript route handler
107106get /hello {
108107 query {
@@ -112,13 +111,15 @@ get /hello {
112111 return { message: f"Hello, {query.name}!" };
113112}
114113"# ;
115-
116- let mut file = fs:: File :: create ( & example_path)
117- . map_err ( |e| format ! ( "Failed to create example file: {}" , e) ) ?;
118-
119- file. write_all ( example_content. as_bytes ( ) )
120- . map_err ( |e| format ! ( "Failed to write to example file: {}" , e) ) ?;
121-
114+
115+ let mut file = fs:: File :: create ( & example_path) . map_err ( |e| {
116+ format ! ( "Failed to create example file: {}" , e)
117+ } ) ?;
118+
119+ file. write_all ( example_content. as_bytes ( ) ) . map_err ( |e| {
120+ format ! ( "Failed to write to example file: {}" , e)
121+ } ) ?;
122+
122123 Ok ( ( ) )
123124 }
124125
@@ -128,7 +129,7 @@ get /hello {
128129 result. push_str ( "├── routes/\n " ) ;
129130 result. push_str ( "│ └── index.ai\n " ) ;
130131 result. push_str ( "└── project.toml\n " ) ;
131-
132+
132133 result
133134 }
134135}
@@ -144,75 +145,65 @@ mod tests {
144145 // Use tempdir to ensure test files are cleaned up
145146 let temp_dir = tempdir ( ) . unwrap ( ) ;
146147 let temp_path = temp_dir. path ( ) ;
147-
148+
148149 // Create a test project in the temp directory
149150 let project_name = "test_project" ;
151+
152+ // Create an absolute path for the project
150153 let project_path = temp_path. join ( project_name) ;
151-
152- // Run in the temp directory
153- std:: env:: set_current_dir ( temp_path) . unwrap ( ) ;
154-
154+
155+ // Create a generator with the project name
155156 let generator = ProjectGenerator :: new ( project_name) ;
157+
158+ // Override the project path for testing
159+ let generator = ProjectGenerator {
160+ project_name : project_name. to_string ( ) ,
161+ project_path : project_path. clone ( ) ,
162+ } ;
163+
156164 let result = generator. generate ( ) ;
157-
165+
158166 assert ! ( result. is_ok( ) , "Project generation failed: {:?}" , result) ;
159-
167+
160168 // Verify project structure
161169 assert ! ( project_path. exists( ) , "Project directory not created" ) ;
162- assert ! (
163- project_path. join( "lib" ) . exists( ) ,
164- "lib directory not created"
165- ) ;
166- assert ! (
167- project_path. join( "routes" ) . exists( ) ,
168- "routes directory not created"
169- ) ;
170- assert ! (
171- project_path. join( "project.toml" ) . exists( ) ,
172- "project.toml not created"
173- ) ;
174- assert ! (
175- project_path. join( "routes/index.ai" ) . exists( ) ,
176- "Example file not created"
177- ) ;
178-
170+ assert ! ( project_path. join( "lib" ) . exists( ) , "lib directory not created" ) ;
171+ assert ! ( project_path. join( "routes" ) . exists( ) , "routes directory not created" ) ;
172+ assert ! ( project_path. join( "project.toml" ) . exists( ) , "project.toml not created" ) ;
173+ assert ! ( project_path. join( "routes/index.ai" ) . exists( ) , "Example file not created" ) ;
174+
179175 // Verify project.toml content
180176 let toml_content = fs:: read_to_string ( project_path. join ( "project.toml" ) ) . unwrap ( ) ;
181177 assert ! ( toml_content. contains( & format!( "name = \" {}\" " , project_name) ) ) ;
182178 assert ! ( toml_content. contains( "version = \" 0.1.0\" " ) ) ;
183-
179+
184180 // Verify example file content
185181 let example_content = fs:: read_to_string ( project_path. join ( "routes/index.ai" ) ) . unwrap ( ) ;
186- assert ! ( example_content. contains( "fn index(req, res) " ) ) ;
182+ assert ! ( example_content. contains( "get /hello " ) ) ;
187183 }
188-
184+
189185 #[ test]
190186 fn test_project_already_exists ( ) {
191187 // Use tempdir to ensure test files are cleaned up
192188 let temp_dir = tempdir ( ) . unwrap ( ) ;
193189 let temp_path = temp_dir. path ( ) ;
194-
190+
195191 // Create a directory that will conflict
196192 let project_name = "existing_project" ;
197193 let project_path = temp_path. join ( project_name) ;
198194 fs:: create_dir_all ( & project_path) . unwrap ( ) ;
199-
200- // Run in the temp directory
201- std:: env:: set_current_dir ( temp_path) . unwrap ( ) ;
202-
203- let generator = ProjectGenerator :: new ( project_name) ;
195+
196+ // Create a generator with absolute path
197+ let generator = ProjectGenerator {
198+ project_name : project_name. to_string ( ) ,
199+ project_path,
200+ } ;
201+
204202 let result = generator. generate ( ) ;
205-
206- assert ! (
207- result. is_err( ) ,
208- "Project generation should fail for existing directory"
209- ) ;
203+
204+ assert ! ( result. is_err( ) , "Project generation should fail for existing directory" ) ;
210205 if let Err ( err) = result {
211- assert ! (
212- err. contains( "already exists" ) ,
213- "Wrong error message: {}" ,
214- err
215- ) ;
206+ assert ! ( err. contains( "already exists" ) , "Wrong error message: {}" , err) ;
216207 }
217208 }
218209}
0 commit comments