11import { select } from '@inquirer/prompts' ;
22import { vol } from 'memfs' ;
33import { MEMFS_VOLUME } from '@code-pushup/test-utils' ;
4- import { logger } from '@code-pushup/utils' ;
54import { promptCiProvider , resolveCi } from './ci.js' ;
65import type { ConfigContext } from './types.js' ;
76import { createTree } from './virtual-fs.js' ;
@@ -10,6 +9,11 @@ vi.mock('@inquirer/prompts', () => ({
109 select : vi . fn ( ) ,
1110} ) ) ;
1211
12+ vi . mock ( '@code-pushup/utils' , async importOriginal => ( {
13+ ...( await importOriginal < typeof import ( '@code-pushup/utils' ) > ( ) ) ,
14+ getGitDefaultBranch : vi . fn ( ) . mockResolvedValue ( 'main' ) ,
15+ } ) ) ;
16+
1317describe ( 'promptCiProvider' , ( ) => {
1418 it . each ( [ 'github' , 'gitlab' , 'none' ] as const ) (
1519 'should return %j when --ci %s is provided' ,
@@ -64,6 +68,7 @@ describe('resolveCi', () => {
6468 jobs:
6569 code-pushup:
6670 runs-on: ubuntu-latest
71+ name: Code PushUp
6772 steps:
6873 - name: Clone repository
6974 uses: actions/checkout@v5
@@ -100,6 +105,7 @@ describe('resolveCi', () => {
100105 jobs:
101106 code-pushup:
102107 runs-on: ubuntu-latest
108+ name: Code PushUp
103109 steps:
104110 - name: Clone repository
105111 uses: actions/checkout@v5
@@ -127,30 +133,77 @@ describe('resolveCi', () => {
127133 path : '.gitlab-ci.yml' ,
128134 type : 'CREATE' ,
129135 } ) ;
136+ await expect ( tree . read ( '.gitlab-ci.yml' ) ) . resolves . toMatchInlineSnapshot ( `
137+ "workflow:
138+ rules:
139+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
140+ - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
141+
142+ include:
143+ - https://gitlab.com/code-pushup/gitlab-pipelines-template/-/raw/latest/code-pushup.yml
144+ "
145+ ` ) ;
130146 } ) ;
131147
132- it ( 'should create separate file and log include instruction when .gitlab-ci.yml already exists ' , async ( ) => {
148+ it ( 'should append local include when .gitlab-ci.yml has include array ' , async ( ) => {
133149 vol . fromJSON (
134150 {
135151 'package.json' : '{}' ,
136- '.gitlab-ci.yml' : 'stages :\n - test \n' ,
152+ '.gitlab-ci.yml' : 'include :\n - local: .gitlab/ci/version.yml \n' ,
137153 } ,
138154 MEMFS_VOLUME ,
139155 ) ;
140156 const tree = createTree ( MEMFS_VOLUME ) ;
141157
142158 await resolveCi ( tree , 'gitlab' , STANDALONE_CONTEXT ) ;
143159
144- expect ( tree . listChanges ( ) ) . toPartiallyContain ( {
145- path : 'code-pushup.gitlab-ci.yml' ,
146- type : 'CREATE' ,
147- } ) ;
148- expect ( tree . listChanges ( ) ) . not . toPartiallyContain ( {
149- path : '.gitlab-ci.yml' ,
150- } ) ;
151- expect ( logger . warn ) . toHaveBeenCalledWith (
152- expect . stringContaining ( 'code-pushup.gitlab-ci.yml' ) ,
160+ await expect ( tree . read ( '.gitlab-ci.yml' ) ) . resolves . toMatchInlineSnapshot ( `
161+ "include:
162+ - local: .gitlab/ci/version.yml
163+ - local: .gitlab/ci/code-pushup.gitlab-ci.yml
164+ "
165+ ` ) ;
166+ } ) ;
167+
168+ it ( 'should wrap single include object into array and append' , async ( ) => {
169+ vol . fromJSON (
170+ {
171+ 'package.json' : '{}' ,
172+ '.gitlab-ci.yml' : 'include:\n local: .gitlab/ci/version.yml\n' ,
173+ } ,
174+ MEMFS_VOLUME ,
175+ ) ;
176+ const tree = createTree ( MEMFS_VOLUME ) ;
177+
178+ await resolveCi ( tree , 'gitlab' , STANDALONE_CONTEXT ) ;
179+
180+ await expect ( tree . read ( '.gitlab-ci.yml' ) ) . resolves . toMatchInlineSnapshot ( `
181+ "include:
182+ - local: .gitlab/ci/version.yml
183+ - local: .gitlab/ci/code-pushup.gitlab-ci.yml
184+ "
185+ ` ) ;
186+ } ) ;
187+
188+ it ( 'should create include array when .gitlab-ci.yml has no include key' , async ( ) => {
189+ vol . fromJSON (
190+ {
191+ 'package.json' : '{}' ,
192+ '.gitlab-ci.yml' : 'stages:\n - test\n' ,
193+ } ,
194+ MEMFS_VOLUME ,
153195 ) ;
196+ const tree = createTree ( MEMFS_VOLUME ) ;
197+
198+ await resolveCi ( tree , 'gitlab' , STANDALONE_CONTEXT ) ;
199+
200+ await expect ( tree . read ( '.gitlab-ci.yml' ) ) . resolves . toMatchInlineSnapshot ( `
201+ "stages:
202+ - test
203+ include:
204+ - local: .gitlab/ci/code-pushup.gitlab-ci.yml
205+ "
206+ ` ) ;
154207 } ) ;
155208 } ) ;
156209
0 commit comments