@@ -3,12 +3,14 @@ import {handler} from "../../../src/handler.js";
33import chalk from "chalk-template" ;
44import { initSpawnSpy } from "../../mocks/utils.mock.js" ;
55import { WhenStatics } from "../../mocks/when-statics.js" ;
6+ import { vi } from "vitest" ;
7+ import { GitData } from "../../../src/git-data.js" ;
68
79beforeAll ( ( ) => {
810 initSpawnSpy ( WhenStatics . all ) ;
911} ) ;
1012
11- test . concurrent ( "list-rule-case --list-rule" , async ( ) => {
13+ test . concurrent ( "list-rule-case --list-rule single rule " , async ( ) => {
1214 const writeStreams = new WriteStreamsMock ( ) ;
1315 await handler ( {
1416 cwd : "tests/test-cases/list-rule-case/" ,
@@ -21,7 +23,6 @@ test.concurrent("list-rule-case --list-rule", async () => {
2123 chalk `{blueBright test-job } Run Tests {yellow test } on_success false ` ,
2224 chalk `{blueBright build-job} {yellow build} on_success true {yellow 1 == 1}` ,
2325 ] ;
24- console . log ( writeStreams . stdoutLines )
2526 expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
2627} ) ;
2728
@@ -39,6 +40,129 @@ test.concurrent("list-rule-case --list-rule verify that correct matching rule is
3940 chalk `{blueBright test-job } Run Tests {yellow test } on_success false ` ,
4041 chalk `{blueBright build-job} {yellow build} on_success true {yellow 1 == 1}` ,
4142 ] ;
42- console . log ( writeStreams . stdoutLines )
4343 expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
44- } ) ;
44+ } ) ;
45+
46+ test . concurrent ( "list-rule-case --list-rule verify that matching 'exists' rule is displayed" , async ( ) => {
47+ const writeStreams = new WriteStreamsMock ( ) ;
48+ await handler ( {
49+ cwd : "tests/test-cases/list-rule-case/" ,
50+ listRule : true ,
51+ file : ".gitlab-ci-match-exists-rule.yml" ,
52+ stateDir : ".gitlab-ci-match-exists-rule" ,
53+ } , writeStreams ) ;
54+
55+ const expected = [
56+ chalk `{grey name description} {grey stage when } {grey allow_failure rule}` ,
57+ chalk `{blueBright test-job } Run Tests {yellow test } on_success false ` ,
58+ chalk `{blueBright build-job} {yellow build} on_success true {yellow exists: [integration.test.ts, .gitlab-ci.yml]}` ,
59+ ] ;
60+ expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
61+ } ) ;
62+
63+ test ( "list-rule-case --list-rule --evaluate-rule-changes verify that matching 'changes' rule is displayed" , async ( ) => {
64+ const writeStreams = new WriteStreamsMock ( ) ;
65+
66+ // Mock GitData.changedFiles to return files that match test case
67+ const gitDataSpy = vi . spyOn ( GitData , "changedFiles" ) ;
68+ gitDataSpy . mockReturnValue ( [ ".gitlab-ci.yml" ] ) ;
69+
70+ await handler ( {
71+ cwd : "tests/test-cases/list-rule-case/" ,
72+ listRule : true ,
73+ evaluateRuleChanges : true ,
74+ file : ".gitlab-ci-match-changes-rule.yml" ,
75+ stateDir : ".gitlab-ci-match-changes-rule" ,
76+ } , writeStreams ) ;
77+
78+ const expected = [
79+ chalk `{grey name description} {grey stage when } {grey allow_failure rule}` ,
80+ chalk `{blueBright test-job } Run Tests {yellow test } on_success false ` ,
81+ chalk `{blueBright build-job} {yellow build} on_success true {yellow changes: [.gitlab-ci.yml]}` ,
82+ ] ;
83+ expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
84+
85+ gitDataSpy . mockRestore ( ) ;
86+ } ) ;
87+
88+ test . concurrent ( "list-rule-case --list-rule verify that 'when' rule doesn't display" , async ( ) => {
89+ const writeStreams = new WriteStreamsMock ( ) ;
90+ await handler ( {
91+ cwd : "tests/test-cases/list-rule-case/" ,
92+ listRule : true ,
93+ file : ".gitlab-ci-when-rule.yml" ,
94+ stateDir : ".gitlab-ci-when-rule" ,
95+ } , writeStreams ) ;
96+
97+ const expected = [
98+ chalk `{grey name description} {grey stage when } {grey allow_failure rule}` ,
99+ chalk `{blueBright build-job} Build {yellow build } on_success false ` ,
100+ chalk `{blueBright test-job } {yellow test } on_failure false ` ,
101+ ] ;
102+ expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
103+ } ) ;
104+
105+ test . concurrent ( "list-rule-case --list-rule verify that job combining 'if' and 'exists' displays properly" , async ( ) => {
106+ const writeStreams = new WriteStreamsMock ( ) ;
107+ await handler ( {
108+ cwd : "tests/test-cases/list-rule-case/" ,
109+ listRule : true ,
110+ file : ".gitlab-ci-if-and-exists.yml" ,
111+ stateDir : ".gitlab-ci-if-and-exists" ,
112+ } , writeStreams ) ;
113+
114+ const expected = [
115+ chalk `{grey name description} {grey stage when } {grey allow_failure rule}` ,
116+ chalk `{blueBright docker-compose-up } {yellow deploy} on_success false ` ,
117+ chalk `{blueBright docker-compose-down} {yellow .post } on_success false {yellow 1 == 1 && exists: [integration.test.ts]}` ,
118+ ] ;
119+ expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
120+ } ) ;
121+
122+ test . concurrent ( "list-rule-case --list-rule --evaluate-rule-changes verify that job combining 'if' and 'changes' displays properly" , async ( ) => {
123+ const writeStreams = new WriteStreamsMock ( ) ;
124+
125+ const gitDataSpy = vi . spyOn ( GitData , "changedFiles" ) ;
126+ gitDataSpy . mockReturnValue ( [ "example/fake-file.js" , "other-file.txt" , "one-more-file.json" ] ) ;
127+
128+ await handler ( {
129+ cwd : "tests/test-cases/list-rule-case/" ,
130+ listRule : true ,
131+ evaluateRuleChanges : true ,
132+ file : ".gitlab-ci-if-and-changes.yml" ,
133+ stateDir : ".gitlab-ci-if-and-changes" ,
134+ } , writeStreams ) ;
135+
136+ const expected = [
137+ chalk `{grey name description} {grey stage when } {grey allow_failure rule}` ,
138+ chalk `{blueBright docker-build} {yellow build } on_success false {yellow 1 == 1 && changes: [example/*.js, other-file.txt]}` ,
139+ ] ;
140+ expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
141+
142+ gitDataSpy . mockRestore ( ) ;
143+
144+ } ) ;
145+
146+ test ( "list-rule-case --list-rule --evaluate-rule-changes verify that if changes weren't made, rule doesn't display" , async ( ) => {
147+ const writeStreams = new WriteStreamsMock ( ) ;
148+
149+ const gitDataSpy = vi . spyOn ( GitData , "changedFiles" ) ;
150+ gitDataSpy . mockReturnValue ( [ "non-matching-file.js" , "other-non-matching-file.txt" ] ) ;
151+
152+ await handler ( {
153+ cwd : "tests/test-cases/list-rule-case/" ,
154+ listRule : true ,
155+ evaluateRuleChanges : true ,
156+ file : ".gitlab-ci-if-and-changes.yml" ,
157+ stateDir : ".gitlab-ci-if-and-changes" ,
158+ } , writeStreams ) ;
159+
160+ const expected = [
161+ chalk `{grey name description} {grey stage when } {grey allow_failure rule}` ,
162+ chalk `{blueBright docker-build} {yellow build } on_success false {yellow 2 == 2}` ,
163+ ] ;
164+ expect ( writeStreams . stdoutLines ) . toEqual ( expect . arrayContaining ( expected ) ) ;
165+
166+ gitDataSpy . mockRestore ( ) ;
167+
168+ } ) ;
0 commit comments