From 739b426201ceb5fa5a533dadead2a3386bd2d89b Mon Sep 17 00:00:00 2001 From: Haroon Sheikh Date: Thu, 20 Jul 2023 15:59:37 +0100 Subject: [PATCH] Adds gauge_concepts_dir for defining concepts directory (#2379) --- .github/workflows/tests.yml | 4 +- CONTRIBUTING.md | 4 +- env/env.go | 2 + .../multipleSpecs/multipleSpecs.properties | 3 +- util/fileUtils.go | 56 ++++++++++++------- util/fileUtils_test.go | 47 +++++++++++++++- util/util.go | 15 +++++ util/util_test.go | 10 ++++ version/version.go | 2 +- 9 files changed, 116 insertions(+), 27 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 28907e25f..7581641e3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -63,7 +63,7 @@ jobs: - name: Install Gauge (windows) if: matrix.os == 'windows-latest' run: | - go run build/make.go --install --verbose + go run build/make.go --install --verbose echo "C:\\Program Files\\gauge\\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append - name: Install Gauge (linux) @@ -123,7 +123,7 @@ jobs: - name: Install Gauge (windows) if: matrix.os == 'windows-latest' run: | - go run build/make.go --install --verbose + go run build/make.go --install --verbose echo "C:\\Program Files\\gauge\\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append - name: Install Gauge (linux) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a47c1e728..c0d29c6b3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -84,7 +84,7 @@ If you are trying to write plugins for Gauge or trying to contribute to Gauge co Ex: ```diff // CurrentGaugeVersion represents the current version of Gauge --var CurrentGaugeVersion = &Version{1, 0, 7} -+var CurrentGaugeVersion = &Version{1, 0, 8} +-var CurrentGaugeVersion = &Version{1, 5, 0} ++var CurrentGaugeVersion = &Version{1, 5, 1} ``` diff --git a/env/env.go b/env/env.go index ab2436146..2d4a9fe99 100644 --- a/env/env.go +++ b/env/env.go @@ -25,6 +25,8 @@ import ( const ( // SpecsDir holds the location of spec files SpecsDir = "gauge_specs_dir" + // ConceptsDir holds the location of concept files + ConceptsDir = "gauge_concepts_dir" // GaugeReportsDir holds the location of reports GaugeReportsDir = "gauge_reports_dir" // GaugeEnvironment holds the name of the current environment diff --git a/util/_testdata/proj1/env/multipleSpecs/multipleSpecs.properties b/util/_testdata/proj1/env/multipleSpecs/multipleSpecs.properties index ca87e73a0..aac9ef419 100644 --- a/util/_testdata/proj1/env/multipleSpecs/multipleSpecs.properties +++ b/util/_testdata/proj1/env/multipleSpecs/multipleSpecs.properties @@ -1 +1,2 @@ -gauge_specs_dir=spec1, spec2, spec3 \ No newline at end of file +gauge_specs_dir=spec1, spec2, spec3 +gauge_concepts_dir=dir1, dir2, dir3 diff --git a/util/fileUtils.go b/util/fileUtils.go index 9f48d2c26..913f11995 100644 --- a/util/fileUtils.go +++ b/util/fileUtils.go @@ -147,12 +147,13 @@ func IsDir(path string) bool { return fileInfo.IsDir() } -// GetSpecFiles returns the list of spec files present at the given path. -// If the path itself represents a spec file, it returns the same. var exitWithMessage = func(message string) { logger.Errorf(true, message) os.Exit(1) } + +// GetSpecFiles returns the list of spec files present at the given path. +// If the path itself represents a spec file, it returns the same. var GetSpecFiles = func(paths []string) []string { var specFiles []string for _, path := range paths { @@ -173,28 +174,43 @@ var GetSpecFiles = func(paths []string) []string { return specFiles } -// GetConceptFiles returns the list of concept files present in the PROJECTROOT +func findConceptFiles(paths []string) []string { + var conceptFiles []string + for _, path := range paths { + var conceptPath = strings.TrimSpace(path) + if !filepath.IsAbs(conceptPath) { + conceptPath = filepath.Join(config.ProjectRoot, conceptPath) + } + absPath, err := filepath.Abs(conceptPath) + if err != nil { + logger.Fatalf(true, "Error getting absolute concept path. %v", err) + } + if !common.FileExists(absPath) { + exitWithMessage(fmt.Sprintf("No such file or diretory: %s", absPath)) + } + conceptFiles = append(conceptFiles, FindConceptFilesIn(absPath)...) + } + return conceptFiles +} + +// GetConceptFiles It returns the list of concept files +// It returns concept files from gauge_concepts_dir if present +// It returns concept files from projectRoot otherwise var GetConceptFiles = func() []string { + conceptPaths := GetConceptsPaths() + if len(conceptPaths) > 0 { + return removeDuplicateValues(findConceptFiles(conceptPaths)) + } projRoot := config.ProjectRoot if projRoot == "" { logger.Fatalf(true, "Failed to get project root.") } - absPath, err := filepath.Abs(projRoot) - if err != nil { - logger.Fatalf(true, "Error getting absolute path. %v", err) - } - files := FindConceptFilesIn(absPath) - var specFromProperties = os.Getenv(env.SpecsDir) - if specFromProperties == "" { - return files - } - var specDirectories = strings.Split(specFromProperties, ",") - for _, dir := range specDirectories { - absSpecPath, err := filepath.Abs(strings.TrimSpace(dir)) - if err != nil { - logger.Fatalf(true, "Error getting absolute path. %v", err) - } - files = append(files, FindConceptFilesIn(absSpecPath)...) + absProjRoot, _ := filepath.Abs(projRoot) + files := findConceptFiles([]string{absProjRoot}) + specDirFromProperties := os.Getenv(env.SpecsDir) + if specDirFromProperties != "" { + specDirectories := strings.Split(specDirFromProperties, ",") + files = append(files, findConceptFiles(specDirectories)...) } return removeDuplicateValues(files) } @@ -230,7 +246,7 @@ func GetPathToFile(path string) string { } gaugeDataDir := env.GaugeDataDir() - if gaugeDataDir != "." && filepath.IsAbs((env.GaugeDataDir())) { + if gaugeDataDir != "." && filepath.IsAbs(env.GaugeDataDir()) { logger.Warningf(true, "'gauge_data_dir' property must be relative to Project Root. Found absolute path: %s", gaugeDataDir) } diff --git a/util/fileUtils_test.go b/util/fileUtils_test.go index d8589afaf..f161ef195 100644 --- a/util/fileUtils_test.go +++ b/util/fileUtils_test.go @@ -147,6 +147,7 @@ func (s *MySuite) TestFindAllConceptFilesInNestedDir(c *C) { } func (s *MySuite) TestGetConceptFiles(c *C) { + os.Clearenv() config.ProjectRoot = "_testdata" specsDir, _ := filepath.Abs(filepath.Join("_testdata", "specs")) @@ -164,17 +165,42 @@ func (s *MySuite) TestGetConceptFiles(c *C) { } func (s *MySuite) TestGetConceptFilesWhenSpecDirIsOutsideProjectRoot(c *C) { + os.Clearenv() config.ProjectRoot = "_testdata" - os.Setenv(env.SpecsDir, "_testSpecDir") + os.Setenv(env.SpecsDir, "../_testSpecDir") c.Assert(len(GetConceptFiles()), Equals, 3) } func (s *MySuite) TestGetConceptFilesWhenSpecDirIsWithInProject(c *C) { + os.Clearenv() config.ProjectRoot = "_testdata" os.Setenv(env.SpecsDir, "_testdata/specs") c.Assert(len(GetConceptFiles()), Equals, 2) } +func (s *MySuite) TestGetConceptFilesWhenConceptsDirSet(c *C) { + os.Clearenv() + config.ProjectRoot = "_testdata" + c.Assert(len(GetConceptFiles()), Equals, 2) + + os.Setenv(env.ConceptsDir, filepath.Join("specs", "concept1.cpt")) + c.Assert(len(GetConceptFiles()), Equals, 1) + + os.Setenv(env.ConceptsDir, filepath.Join("specs", "subdir")) + c.Assert(len(GetConceptFiles()), Equals, 1) + + conceptPath, _ := filepath.Abs(filepath.Join("_testdata", "specs", "subdir", "concept2.cpt")) + os.Setenv(env.ConceptsDir, conceptPath) + c.Assert(len(GetConceptFiles()), Equals, 1) +} + +func (s *MySuite) TestGetConceptFilesWhenConceptDirIsOutsideProjectRoot(c *C) { + os.Clearenv() + config.ProjectRoot = "_testdata" + os.Setenv(env.ConceptsDir, "../_testSpecDir,../_testdata/specs") + c.Assert(len(GetConceptFiles()), Equals, 3) +} + func (s *MySuite) TestFindAllNestedDirs(c *C) { nested1 := filepath.Join(dir, "nested") nested2 := filepath.Join(dir, "nested2") @@ -261,6 +287,25 @@ func (s *MySuite) TestGetSpecFilesWhenSpecsDirDoesNotExists(c *C) { c.Assert(expectedErrorMessage, Equals, "Specs directory dir1 does not exist.") } +func (s *MySuite) TestGetConceptFilesWhenConceptsDirDoesNotExists(c *C) { + os.Clearenv() + var expectedErrorMessage string + exitWithMessage = func(message string) { + expectedErrorMessage = message + } + config.ProjectRoot = "_testdata" + + os.Setenv(env.SpecsDir, "specs2") + GetConceptFiles() + directory, _ := filepath.Abs(filepath.Join(config.ProjectRoot, "specs2")) + c.Assert(expectedErrorMessage, Equals, fmt.Sprintf("No such file or diretory: %s", directory)) + + os.Setenv(env.SpecsDir, "_testSpecsDir,non-exisitng") + GetConceptFiles() + directory, _ = filepath.Abs(filepath.Join(config.ProjectRoot, "non-exisitng")) + c.Assert(expectedErrorMessage, Equals, fmt.Sprintf("No such file or diretory: %s", directory)) +} + func (s *MySuite) TestGetSpecFilesWhenSpecsDirIsEmpty(c *C) { var expectedErrorMessage string exitWithMessage = func(message string) { diff --git a/util/util.go b/util/util.go index da6413814..3da7df081 100644 --- a/util/util.go +++ b/util/util.go @@ -110,6 +110,21 @@ func GetSpecDirs() []string { return []string{common.SpecsDirectoryName} } +// GetConceptsPaths returns the concepts directory. +// It checks whether the environment variable for gauge_concepts_dir is set. +// It returns specs directories otherwise +func GetConceptsPaths() []string { + var conceptDirs []string + var conceptsDirFromProperties = os.Getenv(env.ConceptsDir) + if conceptsDirFromProperties != "" { + conceptDirs = strings.Split(conceptsDirFromProperties, ",") + for index, ele := range conceptDirs { + conceptDirs[index] = strings.TrimSpace(ele) + } + } + return conceptDirs +} + func ListContains(list []string, val string) bool { for _, s := range list { if s == val { diff --git a/util/util_test.go b/util/util_test.go index d4422efe0..61866adb3 100644 --- a/util/util_test.go +++ b/util/util_test.go @@ -16,3 +16,13 @@ func (s *MySuite) TestSpecDirEnvVariableAllowsCommaSeparatedList(c *C) { c.Assert(e, Equals, nil) c.Assert(GetSpecDirs(), DeepEquals, []string{"spec1", "spec2", "spec3"}) } + +func (s *MySuite) TestConceptsDirEnvVariableAllowsCommaSeparatedList(c *C) { + c.Skip("This is causing the net/httptest panic in util/httpUtils_test.go fail on windows.") + os.Clearenv() + config.ProjectRoot = "_testdata/proj1" + + e := env.LoadEnv("multipleSpecs", nil) + c.Assert(e, Equals, nil) + c.Assert(GetConceptsPaths(), DeepEquals, []string{"dir1", "dir2", "dir3"}) +} diff --git a/version/version.go b/version/version.go index d67d99bb5..681efb5a9 100644 --- a/version/version.go +++ b/version/version.go @@ -14,7 +14,7 @@ import ( ) // CurrentGaugeVersion represents the current version of Gauge -var CurrentGaugeVersion = &Version{1, 5, 1} +var CurrentGaugeVersion = &Version{1, 5, 2} // BuildMetadata represents build information of current release (e.g, nightly build information) var BuildMetadata = ""