@@ -491,6 +491,74 @@ def stub_tempfile(name, filename, opts = {})
491491 end
492492 end
493493
494+ describe :fork_repository do
495+ let ( :source_repos_path ) { tmp_repos_path }
496+ let ( :dest_repos_path ) { tmp_repos_path }
497+ let ( :source_repo_name ) { File . join ( 'source-namespace' , repo_name ) }
498+ let ( :dest_repo_name ) { File . join ( '@hashed' , 'aa' , 'bb' , 'xyz.git' ) }
499+ let ( :dest_repo ) { File . join ( dest_repos_path , dest_repo_name ) }
500+ let ( :dest_namespace ) { File . dirname ( dest_repo ) }
501+ let ( :gl_repo_fork ) { build_gitlab_projects ( 'fork-repository' , source_repos_path , source_repo_name , dest_repos_path , dest_repo_name ) }
502+ let ( :gl_projects_import ) { build_gitlab_projects ( 'import-project' , source_repos_path , source_repo_name , 'https://gitlab.com/gitlab-org/gitlab-test.git' ) }
503+
504+ before do
505+ FileUtils . mkdir_p ( dest_repos_path )
506+ gl_projects_import . exec
507+ end
508+
509+ after do
510+ FileUtils . rm_rf ( dest_repos_path )
511+ end
512+
513+ it "should not fork without a source repository path" do
514+ missing_arg = build_gitlab_projects ( 'fork-repository' , tmp_repos_path , source_repo_name )
515+ expect ( $logger) . to receive ( :error ) . with ( "fork-repository failed: no destination repository path provided." )
516+ expect ( missing_arg . exec ) . to be_false
517+ end
518+
519+ it "should not fork without a destination repository path" do
520+ missing_arg = build_gitlab_projects ( 'fork-repository' , tmp_repos_path , source_repo_name , tmp_repos_path )
521+ $logger. should_receive ( :error ) . with ( "fork-repository failed: no destination repository path provided." )
522+ expect ( missing_arg . exec ) . to be_false
523+ end
524+
525+ it "should fork the repository" do
526+ expect ( gl_repo_fork . exec ) . to be_true
527+ expect ( File . exists? ( dest_repo ) ) . to be_true
528+ expect ( File . exists? ( File . join ( dest_repo , 'hooks' , 'pre-receive' ) ) ) . to be_true
529+ expect ( File . exists? ( File . join ( dest_repo , 'hooks' , 'post-receive' ) ) ) . to be_true
530+ end
531+
532+ it "should not fork if a project of the same name already exists" do
533+ # create a fake project at the intended destination
534+ FileUtils . mkdir_p ( dest_repo )
535+
536+ # trying to fork again should fail as the repo already exists
537+ message = "fork-repository failed: destination repository <#{ dest_repo } > already exists."
538+ expect ( $logger) . to receive ( :error ) . with ( message )
539+ expect ( gl_repo_fork . exec ) . to be_false
540+ end
541+
542+ it "should log a fork-project event" do
543+ message = "Forking repository from <#{ File . join ( tmp_repos_path , source_repo_name ) } > to <#{ dest_repo } >."
544+ expect ( $logger) . to receive ( :info ) . with ( message )
545+
546+ expect ( gl_repo_fork . exec ) . to be_true
547+ end
548+
549+ context 'different storages' do
550+ let ( :dest_repos_path ) { File . join ( ROOT_PATH , 'tmp' , 'alternative' ) }
551+
552+ it "should fork the repo" do
553+ expect ( gl_repo_fork . exec ) . to be_true
554+ expect ( File . exists? ( dest_repo ) ) . to be_true
555+ expect ( File . exists? ( File . join ( dest_repo , 'hooks' , 'pre-receive' ) ) ) . to be_true
556+ expect ( File . exists? ( File . join ( dest_repo , 'hooks' , 'post-receive' ) ) ) . to be_true
557+ end
558+ end
559+ end
560+
561+
494562 describe :fork_project do
495563 let ( :source_repo_name ) { File . join ( 'source-namespace' , repo_name ) }
496564 let ( :dest_repo ) { File . join ( tmp_repos_path , 'forked-to-namespace' , repo_name ) }
0 commit comments