@@ -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,47 @@ 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 ( ) . read_to_string ( & mut cargo_content) . unwrap ( ) ;
100+ // Check that Cargo.toml has a bin section pointing to the correct location (if needed)
101+ if needs_bin_section {
102+ assert ! ( cargo_content. contains( r#"[[bin]]"# ) ) ;
103+ assert_that ( & paths:: root ( ) . join ( "foo/src/main.rs" ) , is_not ( existing_file ( ) ) ) ;
104+ } else {
105+ assert ! ( !cargo_content. contains( r#"[[bin]]"# ) ) ;
106+ assert_that ( & paths:: root ( ) . join ( "foo/src/main.rs" ) , existing_file ( ) ) ;
107+ }
97108}
98109
99110#[ test]
100111fn bin_already_exists_explicit ( ) {
101- bin_already_exists ( true , "src/main.rs" )
112+ bin_already_exists ( true , "src/main.rs" , false )
102113}
103114
104115#[ test]
105116fn bin_already_exists_implicit ( ) {
106- bin_already_exists ( false , "src/main.rs" )
117+ bin_already_exists ( false , "src/main.rs" , false )
107118}
108119
109120#[ test]
110121fn bin_already_exists_explicit_nosrc ( ) {
111- bin_already_exists ( true , "main.rs" )
122+ bin_already_exists ( true , "main.rs" , true )
112123}
113124
114125#[ test]
115126fn bin_already_exists_implicit_nosrc ( ) {
116- bin_already_exists ( false , "main.rs" )
127+ bin_already_exists ( false , "main.rs" , true )
117128}
118129
119130#[ test]
120131fn bin_already_exists_implicit_namenosrc ( ) {
121- bin_already_exists ( false , "foo.rs" )
132+ bin_already_exists ( false , "foo.rs" , true )
122133}
123134
124135#[ test]
125136fn bin_already_exists_implicit_namesrc ( ) {
126- bin_already_exists ( false , "src/foo.rs" )
137+ bin_already_exists ( false , "src/foo.rs" , true )
127138}
128139
129140#[ test]
@@ -190,7 +201,7 @@ cannot automatically generate Cargo.toml as the main target would be ambiguous
190201 assert_that ( & paths:: root ( ) . join ( "foo/Cargo.toml" ) , is_not ( existing_file ( ) ) ) ;
191202}
192203
193- fn lib_already_exists ( rellocation : & str ) {
204+ fn lib_already_exists ( rellocation : & str , needs_lib_section : bool ) {
194205 let path = paths:: root ( ) . join ( "foo" ) ;
195206 fs:: create_dir_all ( & path. join ( "src" ) ) . unwrap ( ) ;
196207
@@ -213,16 +224,38 @@ fn lib_already_exists(rellocation: &str) {
213224 let mut new_content = Vec :: new ( ) ;
214225 File :: open ( & sourcefile_path) . unwrap ( ) . read_to_end ( & mut new_content) . unwrap ( ) ;
215226 assert_eq ! ( Vec :: from( content as & [ u8 ] ) , new_content) ;
227+
228+ let mut cargo_content = String :: new ( ) ;
229+ File :: open ( & paths:: root ( ) . join ( "foo/Cargo.toml" ) ) . unwrap ( ) . read_to_string ( & mut cargo_content) . unwrap ( ) ;
230+ // Check that Cargo.toml has a lib section pointing to the correct location (if needed)
231+ if needs_lib_section {
232+ assert ! ( cargo_content. contains( r#"[lib]"# ) ) ;
233+ assert_that ( & paths:: root ( ) . join ( "foo/src/lib.rs" ) , is_not ( existing_file ( ) ) ) ;
234+ } else {
235+ assert ! ( !cargo_content. contains( r#"[lib]"# ) ) ;
236+ assert_that ( & paths:: root ( ) . join ( "foo/src/lib.rs" ) , existing_file ( ) ) ;
237+ }
238+
216239}
217240
218241#[ test]
219242fn lib_already_exists_src ( ) {
220- lib_already_exists ( "src/lib.rs" )
243+ lib_already_exists ( "src/lib.rs" , false )
221244}
222245
223246#[ test]
224247fn lib_already_exists_nosrc ( ) {
225- lib_already_exists ( "lib.rs" )
248+ lib_already_exists ( "lib.rs" , true )
249+ }
250+
251+ #[ test]
252+ fn no_lib_already_exists_src_add_lib_section ( ) {
253+ lib_already_exists ( "src/foo.rs" , true )
254+ }
255+
256+ #[ test]
257+ fn no_lib_already_exists_nosrc_add_lib_section ( ) {
258+ lib_already_exists ( "foo.rs" , true )
226259}
227260
228261#[ test]
0 commit comments