@@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
33
44use handlebars:: Handlebars ;
55use serde:: { Deserialize , Serialize } ;
6+ use tempfile:: TempDir ;
67
78use crate :: builder:: { BenchmarkData , GoPackage } ;
89use crate :: utils;
@@ -52,26 +53,9 @@ struct TemplateData {
5253 module_name : String ,
5354}
5455
55- pub fn run ( package : & GoPackage ) -> anyhow:: Result < ( PathBuf , PathBuf ) > {
56- // TODO:
57- // [x] 1. Symlink the files to /tmp -> either just the test files or
58- // [x] 2. Rename the _test.go files to _codspeed.go
59- // [x] 3. Generate the codspeed/runner.go file using the template
60-
56+ pub fn run ( package : & GoPackage ) -> anyhow:: Result < ( TempDir , PathBuf ) > {
6157 // 1. Copy the whole module to a build directory
62- //
63- // FIXME: Use tmp folder
64- let target_dir = PathBuf :: from ( format ! (
65- "/tmp/go-build/build-{}" ,
66- package. name( ) // Path::new(&package.dir)
67- // .file_name()
68- // .unwrap()
69- // .to_string_lossy()
70- ) ) ;
71- // FIXME: Create once and reuse for all the packages
72- if target_dir. exists ( ) {
73- fs:: remove_dir_all ( & target_dir) . context ( "Failed to remove existing target directory" ) ?;
74- }
58+ let target_dir = TempDir :: new ( ) ?;
7559 std:: fs:: create_dir_all ( & target_dir) . context ( "Failed to create target directory" ) ?;
7660 utils:: copy_dir_all ( & package. module . dir , & target_dir) ?;
7761
@@ -90,13 +74,12 @@ pub fn run(package: &GoPackage) -> anyhow::Result<(PathBuf, PathBuf)> {
9074 info ! ( "Relative package path: {}" , relative_package_path. display( ) ) ;
9175
9276 // 2. Find benchmark files and patch only their imports
93- let benchmark_files = find_benchmark_files ( & target_dir, relative_package_path, files) ?;
77+ let benchmark_files = find_benchmark_files ( target_dir. path ( ) , relative_package_path, files) ?;
9478 patcher:: patch_imports ( & target_dir, benchmark_files) ?;
9579
9680 // 3. Rename the _test.go files to _codspeed.go
97-
9881 for file in files {
99- let old_path = target_dir. join ( relative_package_path) . join ( file) ;
82+ let old_path = target_dir. path ( ) . join ( relative_package_path) . join ( file) ;
10083 let new_path = old_path. with_file_name (
10184 old_path
10285 . file_name ( )
@@ -118,24 +101,25 @@ pub fn run(package: &GoPackage) -> anyhow::Result<(PathBuf, PathBuf)> {
118101 let template_content = include_str ! ( "template.go" ) ;
119102 handlebars. register_template_string ( "main" , template_content) ?;
120103
121- // TODO:
122- // - Find the benchmarks + their names
123- // - Find their imports and qualified names and everything
124- //
125-
126104 // import <alias> <mod_path>
127105 // { "<name>", <qualified_path> },
128106 let data = TemplateData {
129107 benchmarks : package. benchmarks ( ) ?,
130- module_name : "asdf " . into ( ) , // TODO: Do we need this?
108+ module_name : "codspeed_runner " . into ( ) ,
131109 } ;
132110 let rendered = handlebars. render ( "main" , & data) ?;
133111
134112 let runner_path = target_dir
113+ . path ( )
135114 . join ( relative_package_path)
136115 . join ( "codspeed/runner.go" ) ;
137- fs:: create_dir_all ( target_dir. join ( relative_package_path) . join ( "codspeed" ) )
138- . context ( "Failed to create codspeed directory" ) ?;
116+ fs:: create_dir_all (
117+ target_dir
118+ . path ( )
119+ . join ( relative_package_path)
120+ . join ( "codspeed" ) ,
121+ )
122+ . context ( "Failed to create codspeed directory" ) ?;
139123 fs:: write ( & runner_path, rendered) . context ( "Failed to write runner.go file" ) ?;
140124
141125 Ok ( ( target_dir, runner_path) )
0 commit comments