@@ -63,7 +63,7 @@ fn both_lib_and_bin() {
6363 "[ERROR] can't specify both lib and binary outputs" ) ) ;
6464}
6565
66- fn bin_already_exists ( explicit : bool , rellocation : & str ) {
66+ fn bin_already_exists ( explicit : bool , rellocation : & str , needs_bin_section : bool ) {
6767 let path = paths:: root ( ) . join ( "foo" ) ;
6868 fs:: create_dir_all ( & path. join ( "src" ) ) . unwrap ( ) ;
6969
@@ -94,36 +94,48 @@ fn bin_already_exists(explicit: bool, rellocation: &str) {
9494 let mut new_content = Vec :: new ( ) ;
9595 File :: open ( & sourcefile_path) . unwrap ( ) . read_to_end ( & mut new_content) . unwrap ( ) ;
9696 assert_eq ! ( Vec :: from( content as & [ u8 ] ) , new_content) ;
97+
98+ let mut cargo_content = String :: new ( ) ;
99+ File :: open ( & paths:: root ( ) . join ( "foo/Cargo.toml" ) ) . unwrap ( )
100+ . read_to_string ( & mut cargo_content) . unwrap ( ) ;
101+ // Check that Cargo.toml has a bin section pointing to the correct location (if needed)
102+ if needs_bin_section {
103+ assert ! ( cargo_content. contains( r#"[[bin]]"# ) ) ;
104+ assert_that ( & paths:: root ( ) . join ( "foo/src/main.rs" ) , is_not ( existing_file ( ) ) ) ;
105+ } else {
106+ assert ! ( !cargo_content. contains( r#"[[bin]]"# ) ) ;
107+ assert_that ( & paths:: root ( ) . join ( "foo/src/main.rs" ) , existing_file ( ) ) ;
108+ }
97109}
98110
99111#[ test]
100112fn bin_already_exists_explicit ( ) {
101- bin_already_exists ( true , "src/main.rs" )
113+ bin_already_exists ( true , "src/main.rs" , false )
102114}
103115
104116#[ test]
105117fn bin_already_exists_implicit ( ) {
106- bin_already_exists ( false , "src/main.rs" )
118+ bin_already_exists ( false , "src/main.rs" , false )
107119}
108120
109121#[ test]
110122fn bin_already_exists_explicit_nosrc ( ) {
111- bin_already_exists ( true , "main.rs" )
123+ bin_already_exists ( true , "main.rs" , true )
112124}
113125
114126#[ test]
115127fn bin_already_exists_implicit_nosrc ( ) {
116- bin_already_exists ( false , "main.rs" )
128+ bin_already_exists ( false , "main.rs" , true )
117129}
118130
119131#[ test]
120132fn bin_already_exists_implicit_namenosrc ( ) {
121- bin_already_exists ( false , "foo.rs" )
133+ bin_already_exists ( false , "foo.rs" , true )
122134}
123135
124136#[ test]
125137fn bin_already_exists_implicit_namesrc ( ) {
126- bin_already_exists ( false , "src/foo.rs" )
138+ bin_already_exists ( false , "src/foo.rs" , true )
127139}
128140
129141#[ test]
@@ -190,7 +202,7 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous
190202 assert_that ( & paths:: root ( ) . join ( "foo/Cargo.toml" ) , is_not ( existing_file ( ) ) ) ;
191203}
192204
193- fn lib_already_exists ( rellocation : & str ) {
205+ fn lib_already_exists ( rellocation : & str , needs_lib_section : bool ) {
194206 let path = paths:: root ( ) . join ( "foo" ) ;
195207 fs:: create_dir_all ( & path. join ( "src" ) ) . unwrap ( ) ;
196208
@@ -213,16 +225,39 @@ fn lib_already_exists(rellocation: &str) {
213225 let mut new_content = Vec :: new ( ) ;
214226 File :: open ( & sourcefile_path) . unwrap ( ) . read_to_end ( & mut new_content) . unwrap ( ) ;
215227 assert_eq ! ( Vec :: from( content as & [ u8 ] ) , new_content) ;
228+
229+ let mut cargo_content = String :: new ( ) ;
230+ File :: open ( & paths:: root ( ) . join ( "foo/Cargo.toml" ) ) . unwrap ( )
231+ . read_to_string ( & mut cargo_content) . unwrap ( ) ;
232+ // Check that Cargo.toml has a lib section pointing to the correct location (if needed)
233+ if needs_lib_section {
234+ assert ! ( cargo_content. contains( r#"[lib]"# ) ) ;
235+ assert_that ( & paths:: root ( ) . join ( "foo/src/lib.rs" ) , is_not ( existing_file ( ) ) ) ;
236+ } else {
237+ assert ! ( !cargo_content. contains( r#"[lib]"# ) ) ;
238+ assert_that ( & paths:: root ( ) . join ( "foo/src/lib.rs" ) , existing_file ( ) ) ;
239+ }
240+
216241}
217242
218243#[ test]
219244fn lib_already_exists_src ( ) {
220- lib_already_exists ( "src/lib.rs" )
245+ lib_already_exists ( "src/lib.rs" , false )
221246}
222247
223248#[ test]
224249fn lib_already_exists_nosrc ( ) {
225- lib_already_exists ( "lib.rs" )
250+ lib_already_exists ( "lib.rs" , true )
251+ }
252+
253+ #[ test]
254+ fn no_lib_already_exists_src_add_lib_section ( ) {
255+ lib_already_exists ( "src/foo.rs" , true )
256+ }
257+
258+ #[ test]
259+ fn no_lib_already_exists_nosrc_add_lib_section ( ) {
260+ lib_already_exists ( "foo.rs" , true )
226261}
227262
228263#[ test]
0 commit comments