@@ -36,4 +36,196 @@ describe("git-clone", async () => {
3636 "Cloning fake-url to ~/fake-url..." ,
3737 ] ) ;
3838 } ) ;
39+
40+ it ( "repo_dir should match repo name for https" , async ( ) => {
41+ const url = "https://github.com/coder/coder.git" ;
42+ const state = await runTerraformApply ( import . meta. dir , {
43+ agent_id : "foo" ,
44+ base_dir : "/tmp" ,
45+ url,
46+ } ) ;
47+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/coder" ) ;
48+ expect ( state . outputs . folder_name . value ) . toEqual ( "coder" ) ;
49+ expect ( state . outputs . clone_url . value ) . toEqual ( url ) ;
50+ expect ( state . outputs . web_url . value ) . toEqual ( url ) ;
51+ expect ( state . outputs . branch_name . value ) . toEqual ( "" ) ;
52+ } ) ;
53+
54+ it ( "repo_dir should match repo name for https without .git" , async ( ) => {
55+ const url = "https://github.com/coder/coder" ;
56+ const state = await runTerraformApply ( import . meta. dir , {
57+ agent_id : "foo" ,
58+ base_dir : "/tmp" ,
59+ url,
60+ } ) ;
61+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/coder" ) ;
62+ expect ( state . outputs . clone_url . value ) . toEqual ( url ) ;
63+ expect ( state . outputs . web_url . value ) . toEqual ( url ) ;
64+ expect ( state . outputs . branch_name . value ) . toEqual ( "" ) ;
65+ } ) ;
66+
67+ it ( "repo_dir should match repo name for ssh" , async ( ) => {
68+ const url = "[email protected] :coder/coder.git" ; 69+ const state = await runTerraformApply ( import . meta. dir , {
70+ agent_id : "foo" ,
71+ base_dir : "/tmp" ,
72+ url,
73+ } ) ;
74+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/coder" ) ;
75+ expect ( state . outputs . git_provider . value ) . toEqual ( "" ) ;
76+ expect ( state . outputs . clone_url . value ) . toEqual ( url ) ;
77+ const https_url = "https://github.com/coder/coder.git" ;
78+ expect ( state . outputs . web_url . value ) . toEqual ( https_url ) ;
79+ expect ( state . outputs . branch_name . value ) . toEqual ( "" ) ;
80+ } ) ;
81+
82+ it ( "branch_name should not include query string" , async ( ) => {
83+ const state = await runTerraformApply ( import . meta. dir , {
84+ agent_id : "foo" ,
85+ url : "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch?ref_type=heads" ,
86+ } ) ;
87+ expect ( state . outputs . repo_dir . value ) . toEqual ( "~/repo-tests.log" ) ;
88+ expect ( state . outputs . folder_name . value ) . toEqual ( "repo-tests.log" ) ;
89+ const https_url = "https://gitlab.com/mike.brew/repo-tests.log" ;
90+ expect ( state . outputs . clone_url . value ) . toEqual ( https_url ) ;
91+ expect ( state . outputs . web_url . value ) . toEqual ( https_url ) ;
92+ expect ( state . outputs . branch_name . value ) . toEqual ( "feat/branch" ) ;
93+ } ) ;
94+
95+ it ( "branch_name should not include fragments" , async ( ) => {
96+ const state = await runTerraformApply ( import . meta. dir , {
97+ agent_id : "foo" ,
98+ base_dir : "/tmp" ,
99+ url : "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch#name" ,
100+ } ) ;
101+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/repo-tests.log" ) ;
102+ const https_url = "https://gitlab.com/mike.brew/repo-tests.log" ;
103+ expect ( state . outputs . clone_url . value ) . toEqual ( https_url ) ;
104+ expect ( state . outputs . web_url . value ) . toEqual ( https_url ) ;
105+ expect ( state . outputs . branch_name . value ) . toEqual ( "feat/branch" ) ;
106+ } ) ;
107+
108+ it ( "gitlab url with branch should match" , async ( ) => {
109+ const state = await runTerraformApply ( import . meta. dir , {
110+ agent_id : "foo" ,
111+ base_dir : "/tmp" ,
112+ url : "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch" ,
113+ } ) ;
114+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/repo-tests.log" ) ;
115+ expect ( state . outputs . git_provider . value ) . toEqual ( "gitlab" ) ;
116+ const https_url = "https://gitlab.com/mike.brew/repo-tests.log" ;
117+ expect ( state . outputs . clone_url . value ) . toEqual ( https_url ) ;
118+ expect ( state . outputs . web_url . value ) . toEqual ( https_url ) ;
119+ expect ( state . outputs . branch_name . value ) . toEqual ( "feat/branch" ) ;
120+ } ) ;
121+
122+ it ( "github url with branch should match" , async ( ) => {
123+ const state = await runTerraformApply ( import . meta. dir , {
124+ agent_id : "foo" ,
125+ base_dir : "/tmp" ,
126+ url : "https://github.com/michaelbrewer/repo-tests.log/tree/feat/branch" ,
127+ } ) ;
128+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/repo-tests.log" ) ;
129+ expect ( state . outputs . git_provider . value ) . toEqual ( "github" ) ;
130+ const https_url = "https://github.com/michaelbrewer/repo-tests.log" ;
131+ expect ( state . outputs . clone_url . value ) . toEqual ( https_url ) ;
132+ expect ( state . outputs . web_url . value ) . toEqual ( https_url ) ;
133+ expect ( state . outputs . branch_name . value ) . toEqual ( "feat/branch" ) ;
134+ } ) ;
135+
136+ it ( "self-host git url with branch should match" , async ( ) => {
137+ const state = await runTerraformApply ( import . meta. dir , {
138+ agent_id : "foo" ,
139+ base_dir : "/tmp" ,
140+ url : "https://git.example.com/example/project/-/tree/feat/example" ,
141+ git_providers : `
142+ {
143+ "https://git.example.com/" = {
144+ provider = "gitlab"
145+ }
146+ }` ,
147+ } ) ;
148+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/project" ) ;
149+ expect ( state . outputs . git_provider . value ) . toEqual ( "gitlab" ) ;
150+ const https_url = "https://git.example.com/example/project" ;
151+ expect ( state . outputs . clone_url . value ) . toEqual ( https_url ) ;
152+ expect ( state . outputs . web_url . value ) . toEqual ( https_url ) ;
153+ expect ( state . outputs . branch_name . value ) . toEqual ( "feat/example" ) ;
154+ } ) ;
155+
156+ it ( "handle unsupported git provider configuration" , async ( ) => {
157+ const t = async ( ) => {
158+ await runTerraformApply ( import . meta. dir , {
159+ agent_id : "foo" ,
160+ url : "foo" ,
161+ git_providers : `
162+ {
163+ "https://git.example.com/" = {
164+ provider = "bitbucket"
165+ }
166+ }` ,
167+ } ) ;
168+ } ;
169+ expect ( t ) . toThrow ( 'Allowed values for provider are "github" or "gitlab".' ) ;
170+ } ) ;
171+
172+ it ( "handle unknown git provider url" , async ( ) => {
173+ const url = "https://git.unknown.com/coder/coder" ;
174+ const state = await runTerraformApply ( import . meta. dir , {
175+ agent_id : "foo" ,
176+ base_dir : "/tmp" ,
177+ url,
178+ } ) ;
179+ expect ( state . outputs . repo_dir . value ) . toEqual ( "/tmp/coder" ) ;
180+ expect ( state . outputs . clone_url . value ) . toEqual ( url ) ;
181+ expect ( state . outputs . web_url . value ) . toEqual ( url ) ;
182+ expect ( state . outputs . branch_name . value ) . toEqual ( "" ) ;
183+ } ) ;
184+
185+ it ( "runs with github clone with switch to feat/branch" , async ( ) => {
186+ const state = await runTerraformApply ( import . meta. dir , {
187+ agent_id : "foo" ,
188+ url : "https://github.com/michaelbrewer/repo-tests.log/tree/feat/branch" ,
189+ } ) ;
190+ const output = await executeScriptInContainer ( state , "alpine/git" ) ;
191+ expect ( output . exitCode ) . toBe ( 0 ) ;
192+ expect ( output . stdout ) . toEqual ( [
193+ "Creating directory ~/repo-tests.log..." ,
194+ "Cloning https://github.com/michaelbrewer/repo-tests.log to ~/repo-tests.log on branch feat/branch..." ,
195+ ] ) ;
196+ } ) ;
197+
198+ it ( "runs with gitlab clone with switch to feat/branch" , async ( ) => {
199+ const state = await runTerraformApply ( import . meta. dir , {
200+ agent_id : "foo" ,
201+ url : "https://gitlab.com/mike.brew/repo-tests.log/-/tree/feat/branch" ,
202+ } ) ;
203+ const output = await executeScriptInContainer ( state , "alpine/git" ) ;
204+ expect ( output . exitCode ) . toBe ( 0 ) ;
205+ expect ( output . stdout ) . toEqual ( [
206+ "Creating directory ~/repo-tests.log..." ,
207+ "Cloning https://gitlab.com/mike.brew/repo-tests.log to ~/repo-tests.log on branch feat/branch..." ,
208+ ] ) ;
209+ } ) ;
210+
211+ it ( "runs with github clone with branch_name set to feat/branch" , async ( ) => {
212+ const url = "https://github.com/michaelbrewer/repo-tests.log" ;
213+ const branch_name = "feat/branch" ;
214+ const state = await runTerraformApply ( import . meta. dir , {
215+ agent_id : "foo" ,
216+ url,
217+ branch_name,
218+ } ) ;
219+ expect ( state . outputs . repo_dir . value ) . toEqual ( "~/repo-tests.log" ) ;
220+ expect ( state . outputs . clone_url . value ) . toEqual ( url ) ;
221+ expect ( state . outputs . web_url . value ) . toEqual ( url ) ;
222+ expect ( state . outputs . branch_name . value ) . toEqual ( branch_name ) ;
223+
224+ const output = await executeScriptInContainer ( state , "alpine/git" ) ;
225+ expect ( output . exitCode ) . toBe ( 0 ) ;
226+ expect ( output . stdout ) . toEqual ( [
227+ "Creating directory ~/repo-tests.log..." ,
228+ "Cloning https://github.com/michaelbrewer/repo-tests.log to ~/repo-tests.log on branch feat/branch..." ,
229+ ] ) ;
230+ } ) ;
39231} ) ;
0 commit comments