diff --git a/build/utils/dotnet/solution/solution.go b/build/utils/dotnet/solution/solution.go index 08e7b900..e514c57a 100644 --- a/build/utils/dotnet/solution/solution.go +++ b/build/utils/dotnet/solution/solution.go @@ -154,6 +154,9 @@ func (solution *solution) getProjectsListFromSlns(excludePattern string, log uti return nil, err } if slnProjects != nil { + if len(excludePattern) > 0 { + log.Debug(fmt.Sprintf("Testing to exclude projects by pattern: %s", excludePattern)) + } return solution.parseProjectsFromSolutionFile(slnProjects, excludePattern, log) } return nil, nil @@ -183,17 +186,12 @@ func (solution *solution) parseProjectsFromSolutionFile(slnProjects []string, ex continue } // Exclude projects by pattern. - if len(excludePattern) > 0 { - log.Debug(fmt.Sprintf("Testing to exclude projects by pattern: %s", excludePattern)) - exclude, err := regexp.MatchString(excludePattern, projFilePath) - if err != nil { - log.Error(err) - continue - } - if exclude { - log.Debug(fmt.Sprintf("The path '%s' is excluded", projFilePath)) - continue - } + if exclude, err := isProjectExcluded(projFilePath, excludePattern, log); err != nil { + log.Error(err) + continue + } else if exclude { + log.Debug(fmt.Sprintf("Skipping a project \"%s\", since the path '%s' is excluded", projectName, projFilePath)) + continue } // Looking for .*proj files. if !strings.HasSuffix(filepath.Ext(projFilePath), "proj") { @@ -205,6 +203,13 @@ func (solution *solution) parseProjectsFromSolutionFile(slnProjects []string, ex return projects, nil } +func isProjectExcluded(projFilePath, excludePattern string, log utils.Log) (exclude bool, err error) { + if len(excludePattern) == 0 { + return + } + return regexp.MatchString(excludePattern, projFilePath) +} + func (solution *solution) loadSingleProjectFromDir(log utils.Log) error { // List files with .*proj extension. projFiles, err := utils.ListFilesByFilterFunc(solution.path, func(filePath string) (bool, error) { diff --git a/build/utils/dotnet/solution/solution_test.go b/build/utils/dotnet/solution/solution_test.go index ef981c12..fd9f80ff 100644 --- a/build/utils/dotnet/solution/solution_test.go +++ b/build/utils/dotnet/solution/solution_test.go @@ -142,6 +142,7 @@ func replaceCarriageSign(results []string) { } func TestLoad(t *testing.T) { + // Prepare log := utils.NewDefaultLogger(utils.INFO) wd, err := os.Getwd() if err != nil { @@ -156,13 +157,26 @@ func TestLoad(t *testing.T) { nugetCmd := exec.Command("nuget", "restore", filepath.Join(wd, "tmp", "nugetproj", "solutions", "nugetproj.sln")) assert.NoError(t, nugetCmd.Run()) - // 'nugetproj' contains 2 'packages.config' files for 2 projects - - // 1. located in the project's root directory. - // 2. located in solutions directory. - solution := solution{path: filepath.Join(wd, "testdata", "nugetproj", "solutions"), slnFile: "nugetproj.sln"} - solutions, err := Load(solution.path, solution.slnFile, "", log) - if err != nil { - t.Error(err) + testCases := []struct { + name string + excludePattern string + expectedProjectCount int + }{ + {"noExcludePattern", "", 2}, + {"excludePattern", `(^.*.*/proj1.*/.*.*$)`, 1}, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + // 'nugetproj' contains 2 'packages.config' files for 2 projects - + // 1. located in the project's root directory. + // 2. located in solutions directory. + solution := solution{path: filepath.Join(wd, "testdata", "nugetproj", "solutions"), slnFile: "nugetproj.sln"} + solutions, err := Load(solution.path, solution.slnFile, testCase.excludePattern, log) + if err != nil { + t.Error(err) + } + assert.Equal(t, testCase.expectedProjectCount, len(solutions.GetProjects())) + }) } - assert.Equal(t, 2, len(solutions.GetProjects())) }