@@ -87,6 +87,26 @@ func Test_NewCmdDiff(t *testing.T) {
8787 isTTY : true ,
8888 wantErr : "argument required when using the `--repo` flag" ,
8989 },
90+ {
91+ name : "exclude single pattern" ,
92+ args : "--exclude '*.yml'" ,
93+ isTTY : true ,
94+ want : DiffOptions {
95+ SelectorArg : "" ,
96+ UseColor : true ,
97+ Exclude : []string {"*.yml" },
98+ },
99+ },
100+ {
101+ name : "exclude multiple patterns" ,
102+ args : "--exclude '*.yml' --exclude Makefile" ,
103+ isTTY : true ,
104+ want : DiffOptions {
105+ SelectorArg : "" ,
106+ UseColor : true ,
107+ Exclude : []string {"*.yml" , "Makefile" },
108+ },
109+ },
90110 {
91111 name : "invalid --color argument" ,
92112 args : "--color doublerainbow" ,
@@ -142,6 +162,7 @@ func Test_NewCmdDiff(t *testing.T) {
142162 assert .Equal (t , tt .want .SelectorArg , opts .SelectorArg )
143163 assert .Equal (t , tt .want .UseColor , opts .UseColor )
144164 assert .Equal (t , tt .want .BrowserMode , opts .BrowserMode )
165+ assert .Equal (t , tt .want .Exclude , opts .Exclude )
145166 })
146167 }
147168}
@@ -211,6 +232,48 @@ func Test_diffRun(t *testing.T) {
211232 stubDiffRequest (reg , "application/vnd.github.v3.diff" , fmt .Sprintf (testDiff , "" , "" , "" , "" ))
212233 },
213234 },
235+ {
236+ name : "exclude yml files" ,
237+ opts : DiffOptions {
238+ SelectorArg : "123" ,
239+ UseColor : false ,
240+ Exclude : []string {"*.yml" },
241+ },
242+ wantFields : []string {"number" },
243+ wantStdout : `diff --git a/Makefile b/Makefile
244+ index f2b4805c..3d7bd0f9 100644
245+ --- a/Makefile
246+ +++ b/Makefile
247+ @@ -22,8 +22,8 @@ test:
248+ go test ./...
249+ .PHONY: test
250+
251+ -site:
252+ - git clone https://github.com/github/cli.github.com.git "$@"
253+ +site: bin/gh
254+ + bin/gh repo clone github/cli.github.com "$@"
255+
256+ site-docs: site
257+ git -C site pull
258+ ` ,
259+ httpStubs : func (reg * httpmock.Registry ) {
260+ stubDiffRequest (reg , "application/vnd.github.v3.diff" , fmt .Sprintf (testDiff , "" , "" , "" , "" ))
261+ },
262+ },
263+ {
264+ name : "name only with exclude" ,
265+ opts : DiffOptions {
266+ SelectorArg : "123" ,
267+ UseColor : false ,
268+ NameOnly : true ,
269+ Exclude : []string {"*.yml" },
270+ },
271+ wantFields : []string {"number" },
272+ wantStdout : "Makefile\n " ,
273+ httpStubs : func (reg * httpmock.Registry ) {
274+ stubDiffRequest (reg , "application/vnd.github.v3.diff" , fmt .Sprintf (testDiff , "" , "" , "" , "" ))
275+ },
276+ },
214277 {
215278 name : "web mode" ,
216279 opts : DiffOptions {
@@ -394,6 +457,116 @@ func stubDiffRequest(reg *httpmock.Registry, accept, diff string) {
394457 })
395458}
396459
460+ func Test_filterDiff (t * testing.T ) {
461+ rawDiff := fmt .Sprintf (testDiff , "" , "" , "" , "" )
462+
463+ tests := []struct {
464+ name string
465+ patterns []string
466+ want string
467+ }{
468+ {
469+ name : "exclude yml files" ,
470+ patterns : []string {"*.yml" },
471+ want : `diff --git a/Makefile b/Makefile
472+ index f2b4805c..3d7bd0f9 100644
473+ --- a/Makefile
474+ +++ b/Makefile
475+ @@ -22,8 +22,8 @@ test:
476+ go test ./...
477+ .PHONY: test
478+
479+ -site:
480+ - git clone https://github.com/github/cli.github.com.git "$@"
481+ +site: bin/gh
482+ + bin/gh repo clone github/cli.github.com "$@"
483+
484+ site-docs: site
485+ git -C site pull
486+ ` ,
487+ },
488+ {
489+ name : "exclude Makefile" ,
490+ patterns : []string {"Makefile" },
491+ want : `diff --git a/.github/workflows/releases.yml b/.github/workflows/releases.yml
492+ index 73974448..b7fc0154 100644
493+ --- a/.github/workflows/releases.yml
494+ +++ b/.github/workflows/releases.yml
495+ @@ -44,6 +44,11 @@ jobs:
496+ token: ${{secrets.SITE_GITHUB_TOKEN}}
497+ - name: Publish documentation site
498+ if: "!contains(github.ref, '-')" # skip prereleases
499+ + env:
500+ + GIT_COMMITTER_NAME: cli automation
501+ + GIT_AUTHOR_NAME: cli automation
502+ + GIT_COMMITTER_EMAIL: noreply@github.com
503+ + GIT_AUTHOR_EMAIL: noreply@github.com
504+ run: make site-publish
505+ - name: Move project cards
506+ if: "!contains(github.ref, '-')" # skip prereleases
507+ ` ,
508+ },
509+ {
510+ name : "exclude all files" ,
511+ patterns : []string {"*.yml" , "Makefile" },
512+ want : "" ,
513+ },
514+ {
515+ name : "no matches" ,
516+ patterns : []string {"*.go" },
517+ want : rawDiff ,
518+ },
519+ }
520+ for _ , tt := range tests {
521+ t .Run (tt .name , func (t * testing.T ) {
522+ reader , err := filterDiff (strings .NewReader (rawDiff ), tt .patterns )
523+ require .NoError (t , err )
524+ got , err := io .ReadAll (reader )
525+ require .NoError (t , err )
526+ assert .Equal (t , tt .want , string (got ))
527+ })
528+ }
529+ }
530+
531+ func Test_matchesAny (t * testing.T ) {
532+ tests := []struct {
533+ name string
534+ filename string
535+ patterns []string
536+ want bool
537+ }{
538+ {
539+ name : "exact match" ,
540+ filename : "Makefile" ,
541+ patterns : []string {"Makefile" },
542+ want : true ,
543+ },
544+ {
545+ name : "glob extension" ,
546+ filename : ".github/workflows/releases.yml" ,
547+ patterns : []string {"*.yml" },
548+ want : true ,
549+ },
550+ {
551+ name : "no match" ,
552+ filename : "main.go" ,
553+ patterns : []string {"*.yml" },
554+ want : false ,
555+ },
556+ {
557+ name : "directory glob" ,
558+ filename : ".github/workflows/releases.yml" ,
559+ patterns : []string {".github/*/*" },
560+ want : true ,
561+ },
562+ }
563+ for _ , tt := range tests {
564+ t .Run (tt .name , func (t * testing.T ) {
565+ assert .Equal (t , tt .want , matchesAny (tt .filename , tt .patterns ))
566+ })
567+ }
568+ }
569+
397570func Test_sanitizedReader (t * testing.T ) {
398571 input := strings .NewReader ("\t hello \x1B [m world! ăѣ𝔠ծề\r \n " )
399572 expected := "\t hello \\ u{1b}[m world! ăѣ𝔠ծề\r \n "
0 commit comments