File tree Expand file tree Collapse file tree 3 files changed +37
-4
lines changed
crates/cargo-test-support/src Expand file tree Collapse file tree 3 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -175,6 +175,20 @@ where
175175 ( git_project, repo)
176176}
177177
178+ /// Create a new git repository with a project.
179+ /// Returns both the Project and the git Repository but does not commit.
180+ pub fn new_repo_without_add_and_commit < F > ( name : & str , callback : F ) -> ( Project , git2:: Repository )
181+ where
182+ F : FnOnce ( ProjectBuilder ) -> ProjectBuilder ,
183+ {
184+ let mut git_project = project ( ) . at ( name) ;
185+ git_project = callback ( git_project) ;
186+ let git_project = git_project. build ( ) ;
187+
188+ let repo = init ( & git_project. root ( ) ) ;
189+ ( git_project, repo)
190+ }
191+
178192/// Add all files in the working directory to the git index.
179193pub fn add ( repo : & git2:: Repository ) {
180194 // FIXME(libgit2/libgit2#2514): apparently, `add_all` will add all submodules
Original file line number Diff line number Diff line change @@ -154,11 +154,11 @@ fn check_version_control(config: &Config, opts: &FixOptions) -> CargoResult<()>
154154 if let Ok ( repo) = git2:: Repository :: discover ( config. cwd ( ) ) {
155155 let mut repo_opts = git2:: StatusOptions :: new ( ) ;
156156 repo_opts. include_ignored ( false ) ;
157- if repo. is_empty ( ) ? {
157+ if repo. is_empty ( ) ? && !opts . allow_dirty {
158158 bail ! (
159159 "no commits found in the git repository, and \
160160 `cargo fix` can potentially perform destructive changes; if you'd \
161- like to suppress this error pass `--allow-dirty`, `--allow-staged`, \
161+ like to suppress this error pass `--allow-dirty`, \
162162 or commit your changes"
163163 )
164164 }
Original file line number Diff line number Diff line change @@ -771,6 +771,25 @@ commit the changes to these files:
771771 p. cargo ( "fix --allow-staged" ) . run ( ) ;
772772}
773773
774+ #[ cargo_test]
775+ fn errors_on_empty_repo ( ) {
776+ let ( p, _) =
777+ git:: new_repo_without_add_and_commit ( "foo" , |p| p. file ( "src/lib.rs" , "pub fn foo() {}" ) ) ;
778+
779+ p. cargo ( "fix" )
780+ . with_status ( 101 )
781+ . with_stderr (
782+ "\
783+ error: no commits found in the git repository, \
784+ and `cargo fix` can potentially perform destructive changes; \
785+ if you'd like to suppress this error pass `--allow-dirty`, \
786+ or commit your changes
787+ " ,
788+ )
789+ . run ( ) ;
790+ p. cargo ( "fix --allow-dirty" ) . run ( ) ;
791+ }
792+
774793#[ cargo_test]
775794fn does_not_warn_about_clean_working_directory ( ) {
776795 let p = git:: new ( "foo" , |p| p. file ( "src/lib.rs" , "pub fn foo() {}" ) ) ;
@@ -1715,7 +1734,7 @@ fn fix_with_run_cargo_in_proc_macros() {
17151734 "src/lib.rs" ,
17161735 r#"
17171736 use proc_macro::*;
1718-
1737+
17191738 #[proc_macro]
17201739 pub fn foo(_input: TokenStream) -> TokenStream {
17211740 let output = std::process::Command::new(env!("CARGO"))
@@ -1725,7 +1744,7 @@ fn fix_with_run_cargo_in_proc_macros() {
17251744 eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap());
17261745 println!("{}", std::str::from_utf8(&output.stdout).unwrap());
17271746 "".parse().unwrap()
1728- }
1747+ }
17291748 "# ,
17301749 )
17311750 . file (
You can’t perform that action at this time.
0 commit comments