From 96c2695f43821edc4c0575d3fd867d93eba99c69 Mon Sep 17 00:00:00 2001 From: Nehashri Date: Fri, 19 Apr 2019 12:30:03 +0530 Subject: [PATCH 1/2] initializing logger in persistent pre run, reinitializing after loading environment, #1387 --- .travis.yml | 4 +--- cmd/cmd.go | 3 ++- cmd/daemon.go | 2 +- cmd/docs.go | 2 +- cmd/format.go | 2 +- cmd/refactor.go | 2 +- cmd/run.go | 2 +- cmd/run_test.go | 9 +++++---- cmd/validate.go | 2 +- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index e05ed58ac..490709928 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,11 +7,9 @@ go: addons: apt: packages: - - oracle-java8-set-default + - default-jdk env: - GAUGE_PREFIX="/tmp/gauge" GAUGE_TELEMETRY_ENABLED=false -before_install: - - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export JAVA_HOME="/usr/lib/jvm/java-8-oracle"; fi script: - go run build/make.go - go run build/make.go --test diff --git a/cmd/cmd.go b/cmd/cmd.go index 3c192e51f..d2d6b0abe 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -64,6 +64,7 @@ var ( }, DisableAutoGenTag: true, PersistentPreRun: func(cmd *cobra.Command, args []string) { + initLogger(cmd.Name()) skel.CreateSkelFilesIfRequired() track.Init() config.SetProjectRoot(args) @@ -215,7 +216,7 @@ var exit = func(err error, additionalText string) { os.Exit(0) } -func loadEnvAndInitLogger(cmd *cobra.Command) { +func loadEnvAndReinitLogger(cmd *cobra.Command) { if e := env.LoadEnv(environment); e != nil { logger.Fatalf(true, e.Error()) } diff --git a/cmd/daemon.go b/cmd/daemon.go index 6cb37d3b1..d981f703a 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -45,7 +45,7 @@ var ( if err := config.SetProjectRoot(args); err != nil { exit(err, cmd.UsageString()) } - loadEnvAndInitLogger(cmd) + loadEnvAndReinitLogger(cmd) manifest, _ := manifest.ProjectManifest() language := manifest.Language if lsp { diff --git a/cmd/docs.go b/cmd/docs.go index ecffd378d..04efd95f1 100644 --- a/cmd/docs.go +++ b/cmd/docs.go @@ -32,7 +32,7 @@ var docsCmd = &cobra.Command{ Long: `Generate documentation using specified plugin.`, Example: " gauge docs spectacle specs/", Run: func(cmd *cobra.Command, args []string) { - loadEnvAndInitLogger(cmd) + loadEnvAndReinitLogger(cmd) if err := config.SetProjectRoot(args); err != nil { exit(err, cmd.UsageString()) } diff --git a/cmd/format.go b/cmd/format.go index c047fb91e..c463c69a9 100644 --- a/cmd/format.go +++ b/cmd/format.go @@ -29,7 +29,7 @@ var formatCmd = &cobra.Command{ Long: `Formats the specified spec files.`, Example: " gauge format specs/", Run: func(cmd *cobra.Command, args []string) { - loadEnvAndInitLogger(cmd) + loadEnvAndReinitLogger(cmd) if err := config.SetProjectRoot(args); err != nil { exit(err, cmd.UsageString()) } diff --git a/cmd/refactor.go b/cmd/refactor.go index 63a6ebff8..e991ff2a2 100644 --- a/cmd/refactor.go +++ b/cmd/refactor.go @@ -34,7 +34,7 @@ var refactorCmd = &cobra.Command{ Long: `Refactor steps.`, Example: ` gauge refactor "old step" "new step"`, Run: func(cmd *cobra.Command, args []string) { - loadEnvAndInitLogger(cmd) + loadEnvAndReinitLogger(cmd) if len(args) < 2 { exit(fmt.Errorf("Refactor command needs at least two arguments."), cmd.UsageString()) } diff --git a/cmd/run.go b/cmd/run.go index c1bc396b4..95f88e42c 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -208,7 +208,7 @@ func installMissingPlugins(flag bool) { } func execute(cmd *cobra.Command, args []string) { - loadEnvAndInitLogger(cmd) + loadEnvAndReinitLogger(cmd) if parallel && tagsToFilterForParallelRun != "" && !env.AllowFilteredParallelExecution() { logger.Fatal(true, "Filtered parallel execution is a experimental feature. It can be enabled via allow_filtered_parallel_execution property.") } diff --git a/cmd/run_test.go b/cmd/run_test.go index dc1569e50..8d3f8e951 100644 --- a/cmd/run_test.go +++ b/cmd/run_test.go @@ -19,15 +19,15 @@ import ( "github.com/spf13/cobra" ) -var path = "" +var projectPath = "" func before() { - path, _ = filepath.Abs("_testData") - config.ProjectRoot = path + projectPath, _ = filepath.Abs("_testData") + config.ProjectRoot = projectPath } func after() { - os.RemoveAll(path) + os.RemoveAll(projectPath) } func TestMain(m *testing.M) { @@ -416,6 +416,7 @@ func TestLogLevelCanBeOverriddenForRepeat(t *testing.T) { func TestCorrectFlagsAreSetForRepeat(t *testing.T) { if os.Getenv("TEST_EXITS") == "1" { // expect "env" to be set to "test" + os.MkdirAll(filepath.Join(projectPath, "env", "test"), 0755) execution.ExecuteSpecs = func(s []string) int { f, err := runCmd.Flags().GetString(environmentName) if err != nil { diff --git a/cmd/validate.go b/cmd/validate.go index 13c90ba15..3d750918e 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -35,7 +35,7 @@ var ( Long: `Check for validation and parse errors.`, Example: " gauge validate specs/", Run: func(cmd *cobra.Command, args []string) { - loadEnvAndInitLogger(cmd) + loadEnvAndReinitLogger(cmd) validation.HideSuggestion = hideSuggestion if err := config.SetProjectRoot(args); err != nil { exit(err, cmd.UsageString()) From f6442e1b74c7fbaae028ae7cf78a30abdc081cc0 Mon Sep 17 00:00:00 2001 From: Nehashri Date: Fri, 19 Apr 2019 16:17:50 +0530 Subject: [PATCH 2/2] Adding debug logging statements to init and install flows, #1387 --- plugin/install/install.go | 13 ++++++------- projectInit/init.go | 7 +++++-- projectInit/template.go | 4 ++-- runner/runner.go | 1 + util/httpUtils.go | 3 +++ 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/plugin/install/install.go b/plugin/install/install.go index e0b7cc355..feeca921b 100644 --- a/plugin/install/install.go +++ b/plugin/install/install.go @@ -262,7 +262,6 @@ func installPluginVersion(installDesc *installDescription, versionInstallDescrip tempDir := common.GetTempDir() defer common.Remove(tempDir) - logger.Debugf(true, "Downloading %s", filepath.Base(downloadLink)) pluginZip, err := util.Download(downloadLink, tempDir, "", silent) if err != nil { return installError(fmt.Errorf("Failed to download the plugin. %s", err.Error())) @@ -422,11 +421,11 @@ func constructPluginInstallJSONURL(p string) (string, InstallResult) { if repoURL == "" { return "", installError(fmt.Errorf("Could not find gauge repository url from configuration.")) } - JSONURL := fmt.Sprintf("%s/%s", repoURL, p) + jsonURL := fmt.Sprintf("%s/%s", repoURL, p) if qp := plugin.QueryParams(); qp != "" { - JSONURL += qp + jsonURL += qp } - return JSONURL, installSuccess("") + return jsonURL, installSuccess("") } func (installDesc *installDescription) getVersion(version string) (*versionInstallDescription, error) { @@ -518,8 +517,8 @@ func HandleInstallResult(result InstallResult, pluginName string, exitIfFailure return true } if !result.Success { - if (result.Version != "") { - logger.Errorf(true, "Failed to install plugin '%s' version %s.\nReason: %s", pluginName, result.Version, result.getMessage()) + if result.Version != "" { + logger.Errorf(true, "Failed to install plugin '%s' version %s.\nReason: %s", pluginName, result.Version, result.getMessage()) } else { logger.Errorf(true, "Failed to install plugin '%s'.\nReason: %s", pluginName, result.getMessage()) } @@ -528,7 +527,7 @@ func HandleInstallResult(result InstallResult, pluginName string, exitIfFailure } return false } - if (result.Version != "") { + if result.Version != "" { logger.Infof(true, "Successfully installed plugin '%s' version %s", pluginName, result.Version) } else { logger.Infof(true, "Successfully installed plugin '%s'", pluginName) diff --git a/projectInit/init.go b/projectInit/init.go index a420219fe..2e637ea7c 100644 --- a/projectInit/init.go +++ b/projectInit/init.go @@ -38,7 +38,9 @@ type templateMetadata struct { func initializeTemplate(templateName string) error { tempDir := common.GetTempDir() defer util.Remove(tempDir) - unzippedTemplate, err := util.DownloadAndUnzip(getTemplateURL(templateName), tempDir) + templateURL := getTemplateURL(templateName) + logger.Debugf(true, "Initializing template from %s", templateURL) + unzippedTemplate, err := util.DownloadAndUnzip(templateURL, tempDir) if err != nil { return err } @@ -71,6 +73,7 @@ func initializeTemplate(templateName string) error { } if metadata.PostInstallCmd != "" { + logger.Debugf(true, "Running post install command %s", metadata.PostInstallCmd) command := strings.Fields(metadata.PostInstallCmd) cmd, err := common.ExecuteSystemCommand(command, wd, os.Stdout, os.Stderr) if err != nil { @@ -84,7 +87,7 @@ func initializeTemplate(templateName string) error { return err } } - fmt.Printf("Successfully initialized the project. %s\n", metadata.PostInstallMsg) + logger.Infof(true, "Successfully initialized the project. %s\n", metadata.PostInstallMsg) util.Remove(metadataFile) return nil diff --git a/projectInit/template.go b/projectInit/template.go index 5bbb10988..0ab572e7d 100644 --- a/projectInit/template.go +++ b/projectInit/template.go @@ -24,11 +24,11 @@ func ListTemplates() { templatesURL := config.GaugeTemplatesUrl() _, err := common.UrlExists(templatesURL) if err != nil { - logger.Fatalf(true, "Gauge templates URL is not reachable: %s", err.Error()) + logger.Fatalf(true, "Gauge templates URL %s is not reachable: %s", templatesURL, err.Error()) } res, err := http.Get(templatesURL) if err != nil { - logger.Fatalf(true, "Error occurred while downloading templates list: %s", err.Error()) + logger.Fatalf(true, "Error occurred while downloading templates list from %s: %s", templatesURL, err.Error()) } defer res.Body.Close() if res.StatusCode >= 400 { diff --git a/runner/runner.go b/runner/runner.go index c7d4663d1..c334ffe2d 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -177,6 +177,7 @@ func ExecuteInitHookForRunner(language string) error { languageJSONFilePath, err := plugin.GetLanguageJSONFilePath(language) runnerDir := filepath.Dir(languageJSONFilePath) + logger.Debugf(true, "Running init hook command => %s", command) cmd, err := common.ExecuteCommand(command, runnerDir, os.Stdout, os.Stderr) if err != nil { diff --git a/util/httpUtils.go b/util/httpUtils.go index f8a33e37a..43ada58e9 100644 --- a/util/httpUtils.go +++ b/util/httpUtils.go @@ -24,6 +24,8 @@ import ( "os" "path/filepath" + "github.com/getgauge/gauge/logger" + "github.com/getgauge/common" ) @@ -63,6 +65,7 @@ func Download(url, targetDir, fileName string, silent bool) (string, error) { } targetFile := filepath.Join(targetDir, fileName) + logger.Debugf(true, "Downloading %s", url) resp, err := http.Get(url) if err != nil { return "", err