github.com/saucelabs/saucectl@v0.175.1/internal/playwright/grep/grep_test.go (about)

     1  package grep
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  	"testing/fstest"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"golang.org/x/exp/maps"
    10  )
    11  
    12  func TestMatchFiles(t *testing.T) {
    13  	type testCase struct {
    14  		name          string
    15  		grep          string
    16  		grepInvert    string
    17  		wantMatched   []string
    18  		wantUnmatched []string
    19  	}
    20  
    21  	mockFS := fstest.MapFS{
    22  		"demo-todo.spec.js": {
    23  			Data: []byte(`
    24  test.describe('New Todo', () => {
    25    test('should allow me to add todo items @query', async ({ page }) => {
    26    });
    27  
    28    test('should allow me to add multiple todo items @save', async ({ page }) => {
    29    });
    30  });
    31  `),
    32  		},
    33  		"demo-step.spec.js": {
    34  			Data: []byte(`
    35  test.describe('New Step', () => {
    36    test('should allow me to add one step @fast @unique', async ({ page }) => {
    37    });
    38  
    39    test('should allow me to add multiple steps @slow @unique', async ({ page }) => {
    40    });
    41  });
    42  `),
    43  		},
    44  	}
    45  
    46  	testCases := []testCase{
    47  		{
    48  			name:          "Base",
    49  			grep:          "New Todo",
    50  			wantMatched:   []string{"demo-todo.spec.js"},
    51  			wantUnmatched: []string{"demo-step.spec.js"},
    52  		},
    53  		{
    54  			name:          "tag",
    55  			grep:          "@fast",
    56  			wantMatched:   []string{"demo-step.spec.js"},
    57  			wantUnmatched: []string{"demo-todo.spec.js"},
    58  		},
    59  		{
    60  			name:          "filename",
    61  			grep:          "demo-step",
    62  			wantMatched:   []string{"demo-step.spec.js"},
    63  			wantUnmatched: []string{"demo-todo.spec.js"},
    64  		},
    65  		{
    66  			name:          "tags same spec",
    67  			grep:          "@fast|@slow",
    68  			wantMatched:   []string{"demo-step.spec.js"},
    69  			wantUnmatched: []string{"demo-todo.spec.js"},
    70  		},
    71  		{
    72  			name:          "tags across specs",
    73  			grep:          "@fast|@save",
    74  			wantMatched:   []string{"demo-todo.spec.js", "demo-step.spec.js"},
    75  			wantUnmatched: []string(nil),
    76  		},
    77  		{
    78  			name: "combined tag - not found",
    79  			// Note: example pattern should be "(?=.*@fast)(?=.*@slow)"
    80  			// But Go RegExp library does not support the Perl Look Around.
    81  			grep:          "(.*@fast)(.*)(.*@slow)",
    82  			wantMatched:   []string(nil),
    83  			wantUnmatched: []string{"demo-todo.spec.js", "demo-step.spec.js"},
    84  		},
    85  		{
    86  			name: "combined tag",
    87  			// Note: example pattern should be "(?=.*@fast)(?=.*@unique)"
    88  			// But Go RegExp library does not support the Perl Look Around.
    89  			grep:          "(.*@fast)(.*)(.*@unique)",
    90  			wantMatched:   []string{"demo-step.spec.js"},
    91  			wantUnmatched: []string{"demo-todo.spec.js"},
    92  		},
    93  		{
    94  			name:          "grepInvert",
    95  			grep:          "New Todo",
    96  			grepInvert:    "demo-todo",
    97  			wantMatched:   []string(nil),
    98  			wantUnmatched: []string{"demo-todo.spec.js", "demo-step.spec.js"},
    99  		},
   100  	}
   101  
   102  	for _, tt := range testCases {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			matched, unmatched := MatchFiles(mockFS, maps.Keys(mockFS), tt.grep, tt.grepInvert)
   105  			sort.Slice(matched, func(i, j int) bool {
   106  				return matched[i] > matched[j]
   107  			})
   108  			sort.Slice(unmatched, func(i, j int) bool {
   109  				return unmatched[i] > unmatched[j]
   110  			})
   111  
   112  			if len(mockFS) != len(unmatched)+len(matched) {
   113  				t.Errorf("MatchFiles: unexpected lenght of results. got: %d, want: %d", len(unmatched)+len(matched), len(mockFS))
   114  
   115  			}
   116  			if diff := cmp.Diff(matched, tt.wantMatched); diff != "" {
   117  				t.Errorf("MatchFiles: difference in matched: %s", diff)
   118  			}
   119  			if diff := cmp.Diff(unmatched, tt.wantUnmatched); diff != "" {
   120  				t.Errorf("MatchFiles: difference in unmatched: %s", diff)
   121  			}
   122  
   123  		})
   124  	}
   125  }