Best Rod code snippet using utils.Exec
maven_test.go
Source:maven_test.go
...24func newMockUtils(downloadShouldFail bool) mockUtils {25 utils := mockUtils{shouldFail: downloadShouldFail, FilesMock: &mock.FilesMock{}}26 return utils27}28func TestExecute(t *testing.T) {29 t.Run("should return stdOut", func(t *testing.T) {30 expectedOutput := "mocked output"31 execMockRunner := mock.ExecMockRunner{}32 execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode": "mocked output"}33 opts := ExecuteOptions{PomPath: "pom.xml", ReturnStdout: true}34 mavenOutput, _ := Execute(&opts, &execMockRunner)35 assert.Equal(t, expectedOutput, mavenOutput)36 })37 t.Run("should not return stdOut", func(t *testing.T) {38 expectedOutput := ""39 execMockRunner := mock.ExecMockRunner{}40 execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode": "mocked output"}41 opts := ExecuteOptions{PomPath: "pom.xml", ReturnStdout: false}42 mavenOutput, _ := Execute(&opts, &execMockRunner)43 assert.Equal(t, expectedOutput, mavenOutput)44 })45 t.Run("should log that command failed if executing maven failed", func(t *testing.T) {46 execMockRunner := mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"mvn --file pom.xml -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode": errors.New("error case")}}47 opts := ExecuteOptions{PomPath: "pom.xml", ReturnStdout: false}48 output, err := Execute(&opts, &execMockRunner)49 assert.EqualError(t, err, "failed to run executable, command: '[mvn --file pom.xml -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode]', error: error case")50 assert.Equal(t, "", output)51 })52 t.Run("should have all configured parameters in the exec call", func(t *testing.T) {53 execMockRunner := mock.ExecMockRunner{}54 opts := ExecuteOptions{PomPath: "pom.xml", ProjectSettingsFile: "settings.xml",55 GlobalSettingsFile: "anotherSettings.xml", M2Path: ".m2/",56 Goals: []string{"flatten", "install"}, Defines: []string{"-Da=b"},57 Flags: []string{"-q"}, LogSuccessfulMavenTransfers: true,58 ReturnStdout: false}59 expectedParameters := []string{"--global-settings", "anotherSettings.xml", "--settings", "settings.xml",60 "-Dmaven.repo.local=.m2/", "--file", "pom.xml", "-q", "-Da=b", "--batch-mode",61 "flatten", "install"}62 mavenOutput, _ := Execute(&opts, &execMockRunner)63 assert.Equal(t, len(expectedParameters), len(execMockRunner.Calls[0].Params))64 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: expectedParameters}, execMockRunner.Calls[0])65 assert.Equal(t, "", mavenOutput)66 })67}68func TestEvaluate(t *testing.T) {69 t.Run("should evaluate expression", func(t *testing.T) {70 execMockRunner := mock.ExecMockRunner{}71 execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dexpression=project.groupId -DforceStdout -q -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "com.awesome"}72 result, err := Evaluate(&EvaluateOptions{PomPath: "pom.xml"}, "project.groupId", &execMockRunner)73 if assert.NoError(t, err) {74 assert.Equal(t, "com.awesome", result)75 }76 })77 t.Run("should not evaluate expression", func(t *testing.T) {78 execMockRunner := mock.ExecMockRunner{}79 execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dexpression=project.groupId -DforceStdout -q -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "null object or invalid expression"}80 result, err := Evaluate(&EvaluateOptions{PomPath: "pom.xml"}, "project.groupId", &execMockRunner)81 if assert.EqualError(t, err, "expression 'project.groupId' in file 'pom.xml' could not be resolved") {82 assert.Equal(t, "", result)83 }84 })85}86func TestGetParameters(t *testing.T) {87 t.Run("should resolve configured parameters and download the settings files", func(t *testing.T) {88 utils := newMockUtils(false)89 opts := ExecuteOptions{PomPath: "pom.xml", GlobalSettingsFile: "https://mysettings.com", ProjectSettingsFile: "http://myprojectsettings.com", ReturnStdout: false}90 expectedParameters := []string{91 "--global-settings", ".pipeline/mavenGlobalSettings.xml",92 "--settings", ".pipeline/mavenProjectSettings.xml",93 "--file", "pom.xml",94 "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",95 "--batch-mode"}96 parameters, err := getParametersFromOptions(&opts, &utils)97 if assert.NoError(t, err) {98 assert.Equal(t, len(expectedParameters), len(parameters))99 assert.Equal(t, expectedParameters, parameters)100 if assert.Equal(t, 2, len(utils.requestedUrls)) {101 assert.Equal(t, "https://mysettings.com", utils.requestedUrls[0])102 assert.Equal(t, ".pipeline/mavenGlobalSettings.xml", utils.requestedFiles[0])103 assert.Equal(t, "http://myprojectsettings.com", utils.requestedUrls[1])104 assert.Equal(t, ".pipeline/mavenProjectSettings.xml", utils.requestedFiles[1])105 }106 }107 })108 t.Run("should resolve configured parameters and not download existing settings files", func(t *testing.T) {109 utils := newMockUtils(false)110 utils.AddFile(".pipeline/mavenGlobalSettings.xml", []byte("dummyContent"))111 utils.AddFile(".pipeline/mavenProjectSettings.xml", []byte("dummyContent"))112 opts := ExecuteOptions{PomPath: "pom.xml", GlobalSettingsFile: "https://mysettings.com", ProjectSettingsFile: "http://myprojectsettings.com", ReturnStdout: false}113 expectedParameters := []string{114 "--global-settings", ".pipeline/mavenGlobalSettings.xml",115 "--settings", ".pipeline/mavenProjectSettings.xml",116 "--file", "pom.xml",117 "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn",118 "--batch-mode"}119 parameters, err := getParametersFromOptions(&opts, &utils)120 if assert.NoError(t, err) {121 assert.Equal(t, len(expectedParameters), len(parameters))122 assert.Equal(t, expectedParameters, parameters)123 assert.Equal(t, 0, len(utils.requestedUrls))124 }125 })126}127func TestGetTestModulesExcludes(t *testing.T) {128 t.Run("Should return excludes for unit- and integration-tests", func(t *testing.T) {129 utils := newMockUtils(false)130 utils.AddFile("unit-tests/pom.xml", []byte("dummyContent"))131 utils.AddFile("integration-tests/pom.xml", []byte("dummyContent"))132 expected := []string{"-pl", "!unit-tests", "-pl", "!integration-tests"}133 modulesExcludes := getTestModulesExcludes(&utils)134 assert.Equal(t, expected, modulesExcludes)135 })136 t.Run("Should not return excludes for unit- and integration-tests", func(t *testing.T) {137 utils := newMockUtils(false)138 var expected []string139 modulesExcludes := getTestModulesExcludes(&utils)140 assert.Equal(t, expected, modulesExcludes)141 })142}143func TestMavenInstall(t *testing.T) {144 t.Parallel()145 t.Run("Should return path to jar file", func(t *testing.T) {146 actual := jarFile("app", "my-app")147 assert.Equal(t, filepath.Join("app", "target", "my-app.jar"), actual)148 })149 t.Run("Should return path to war file", func(t *testing.T) {150 actual := warFile("app", "my-app")151 assert.Equal(t, filepath.Join("app", "target", "my-app.war"), actual)152 })153 t.Run("Install a file", func(t *testing.T) {154 execMockRunner := mock.ExecMockRunner{}155 expectedParameters := []string{"-Dfile=app.jar", "-Dpackaging=jar", "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}156 err := InstallFile("app.jar", "pom.xml", "", &execMockRunner)157 assert.NoError(t, err)158 if assert.Equal(t, len(expectedParameters), len(execMockRunner.Calls[0].Params)) {159 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: expectedParameters}, execMockRunner.Calls[0])160 }161 })162 t.Run("Install files in a project", func(t *testing.T) {163 utils := newMockUtils(false)164 utils.AddFile("target/foo.jar", []byte("dummyContent"))165 utils.AddFile("target/foo.war", []byte("dummyContent"))166 utils.AddFile("pom.xml", []byte("<project></project>"))167 options := EvaluateOptions{}168 execMockRunner := mock.ExecMockRunner{}169 execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dexpression=project.build.finalName -DforceStdout -q -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "foo"}170 err := doInstallMavenArtifacts(&execMockRunner, options, &utils)171 assert.NoError(t, err)172 if assert.Equal(t, 5, len(execMockRunner.Calls)) {173 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dflatten.mode=resolveCiFriendliesOnly", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "flatten:flatten"}}, execMockRunner.Calls[0])174 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dexpression=project.packaging", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, execMockRunner.Calls[1])175 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dexpression=project.build.finalName", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, execMockRunner.Calls[2])176 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"-Dfile=" + filepath.Join(".", "target", "foo.jar"), "-Dpackaging=jar", "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, execMockRunner.Calls[3])177 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"-Dfile=" + filepath.Join(".", "target", "foo.war"), "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, execMockRunner.Calls[4])178 }179 })180 t.Run("Install files in a spring-boot project", func(t *testing.T) {181 utils := newMockUtils(false)182 utils.AddFile("target/foo.jar", []byte("dummyContent"))183 utils.AddFile("target/foo.jar.original", []byte("dummyContent"))184 utils.AddFile("pom.xml", []byte("<project></project>"))185 options := EvaluateOptions{}186 execMockRunner := mock.ExecMockRunner{}187 execMockRunner.StdoutReturn = map[string]string{"mvn --file pom.xml -Dexpression=project.build.finalName -DforceStdout -q -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn --batch-mode org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate": "foo"}188 err := doInstallMavenArtifacts(&execMockRunner, options, &utils)189 assert.NoError(t, err)190 if assert.Equal(t, 4, len(execMockRunner.Calls)) {191 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dflatten.mode=resolveCiFriendliesOnly", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "flatten:flatten"}}, execMockRunner.Calls[0])192 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dexpression=project.packaging", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, execMockRunner.Calls[1])193 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"--file", "pom.xml", "-Dexpression=project.build.finalName", "-DforceStdout", "-q", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate"}}, execMockRunner.Calls[2])194 assert.Equal(t, mock.ExecCall{Exec: "mvn", Params: []string{"-Dfile=" + filepath.Join(".", "target", "foo.jar.original"), "-Dpackaging=jar", "-DpomFile=pom.xml", "-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn", "--batch-mode", "install:install-file"}}, execMockRunner.Calls[3])195 }196 })197}...
npm_test.go
Source:npm_test.go
...6 "testing"7)8type npmMockUtilsBundle struct {9 *mock.FilesMock10 execRunner *mock.ExecMockRunner11}12func (u *npmMockUtilsBundle) GetExecRunner() ExecRunner {13 return u.execRunner14}15func newNpmMockUtilsBundle() npmMockUtilsBundle {16 utils := npmMockUtilsBundle{FilesMock: &mock.FilesMock{}, execRunner: &mock.ExecMockRunner{}}17 return utils18}19func TestNpm(t *testing.T) {20 t.Run("find package.json files with one package.json", func(t *testing.T) {21 utils := newNpmMockUtilsBundle()22 utils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))23 options := ExecutorOptions{}24 exec := &Execute{25 Utils: &utils,26 Options: options,27 }28 packageJSONFiles := exec.FindPackageJSONFiles()29 assert.Equal(t, []string{"package.json"}, packageJSONFiles)30 })31 t.Run("find package.json files with two package.json and default filter", func(t *testing.T) {32 utils := newNpmMockUtilsBundle()33 utils.AddFile("package.json", []byte("{}"))34 utils.AddFile(filepath.Join("src", "package.json"), []byte("{}")) // should NOT be filtered out35 utils.AddFile(filepath.Join("node_modules", "package.json"), []byte("{}")) // is filtered out36 utils.AddFile(filepath.Join("gen", "package.json"), []byte("{}")) // is filtered out37 options := ExecutorOptions{}38 exec := &Execute{39 Utils: &utils,40 Options: options,41 }42 packageJSONFiles := exec.FindPackageJSONFiles()43 assert.Equal(t, []string{"package.json", filepath.Join("src", "package.json")}, packageJSONFiles)44 })45 t.Run("find package.json files with two package.json and excludes", func(t *testing.T) {46 utils := newNpmMockUtilsBundle()47 utils.AddFile("package.json", []byte("{}"))48 utils.AddFile(filepath.Join("src", "package.json"), []byte("{}")) // should NOT be filtered out49 utils.AddFile(filepath.Join("notfiltered", "package.json"), []byte("{}")) // should NOT be filtered out50 utils.AddFile(filepath.Join("Path", "To", "filter", "package.json"), []byte("{}")) // should NOT be filtered out51 utils.AddFile(filepath.Join("node_modules", "package.json"), []byte("{}")) // is filtered out52 utils.AddFile(filepath.Join("gen", "package.json"), []byte("{}")) // is filtered out53 utils.AddFile(filepath.Join("filter", "package.json"), []byte("{}")) // is filtered out54 utils.AddFile(filepath.Join("filterPath", "package.json"), []byte("{}")) // is filtered out55 utils.AddFile(filepath.Join("filter", "Path", "To", "package.json"), []byte("{}")) // is filtered out56 options := ExecutorOptions{}57 exec := &Execute{58 Utils: &utils,59 Options: options,60 }61 packageJSONFiles, err := exec.FindPackageJSONFilesWithExcludes([]string{"filter/**", "filterPath/package.json"})62 if assert.NoError(t, err) {63 assert.Equal(t, []string{filepath.Join("Path", "To", "filter", "package.json"), filepath.Join("notfiltered", "package.json"), "package.json", filepath.Join("src", "package.json")}, packageJSONFiles)64 }65 })66 t.Run("find package.json files with script", func(t *testing.T) {67 utils := newNpmMockUtilsBundle()68 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))69 utils.AddFile(filepath.Join("src", "package.json"), []byte("{ \"name\": \"test\" }"))70 utils.AddFile(filepath.Join("test", "package.json"), []byte("{ \"scripts\": { \"test\": \"exit 0\" } }"))71 options := ExecutorOptions{}72 exec := &Execute{73 Utils: &utils,74 Options: options,75 }76 packageJSONFilesWithScript, err := exec.FindPackageJSONFilesWithScript([]string{"package.json", filepath.Join("src", "package.json"), filepath.Join("test", "package.json")}, "ci-lint")77 if assert.NoError(t, err) {78 assert.Equal(t, []string{"package.json"}, packageJSONFilesWithScript)79 }80 })81 t.Run("Install deps for package.json with package-lock.json", func(t *testing.T) {82 utils := newNpmMockUtilsBundle()83 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))84 utils.AddFile("package-lock.json", []byte("{}"))85 options := ExecutorOptions{}86 options.DefaultNpmRegistry = "foo.bar"87 exec := &Execute{88 Utils: &utils,89 Options: options,90 }91 err := exec.install("package.json")92 if assert.NoError(t, err) {93 if assert.Equal(t, 2, len(utils.execRunner.Calls)) {94 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"ci"}}, utils.execRunner.Calls[1])95 }96 }97 })98 t.Run("Install deps for package.json without package-lock.json", func(t *testing.T) {99 utils := newNpmMockUtilsBundle()100 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))101 options := ExecutorOptions{}102 options.DefaultNpmRegistry = "foo.bar"103 exec := &Execute{104 Utils: &utils,105 Options: options,106 }107 err := exec.install("package.json")108 if assert.NoError(t, err) {109 if assert.Equal(t, 2, len(utils.execRunner.Calls)) {110 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"install"}}, utils.execRunner.Calls[1])111 }112 }113 })114 t.Run("Install deps for package.json with yarn.lock", func(t *testing.T) {115 utils := newNpmMockUtilsBundle()116 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))117 utils.AddFile("yarn.lock", []byte("{}"))118 options := ExecutorOptions{}119 options.DefaultNpmRegistry = "foo.bar"120 exec := &Execute{121 Utils: &utils,122 Options: options,123 }124 err := exec.install("package.json")125 if assert.NoError(t, err) {126 if assert.Equal(t, 2, len(utils.execRunner.Calls)) {127 assert.Equal(t, mock.ExecCall{Exec: "yarn", Params: []string{"install", "--frozen-lockfile"}}, utils.execRunner.Calls[1])128 }129 }130 })131 t.Run("Install all deps", func(t *testing.T) {132 utils := newNpmMockUtilsBundle()133 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))134 utils.AddFile("package-lock.json", []byte("{}"))135 utils.AddFile(filepath.Join("src", "package.json"), []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))136 utils.AddFile(filepath.Join("src", "package-lock.json"), []byte("{}"))137 options := ExecutorOptions{}138 options.DefaultNpmRegistry = "foo.bar"139 exec := &Execute{140 Utils: &utils,141 Options: options,142 }143 err := exec.InstallAllDependencies([]string{"package.json", filepath.Join("src", "package.json")})144 if assert.NoError(t, err) {145 if assert.Equal(t, 4, len(utils.execRunner.Calls)) {146 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"ci"}}, utils.execRunner.Calls[1])147 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"ci"}}, utils.execRunner.Calls[3])148 }149 }150 })151 t.Run("check if yarn.lock and package-lock exist", func(t *testing.T) {152 utils := newNpmMockUtilsBundle()153 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))154 utils.AddFile("yarn.lock", []byte("{}"))155 utils.AddFile("package-lock.json", []byte("{}"))156 options := ExecutorOptions{}157 exec := &Execute{158 Utils: &utils,159 Options: options,160 }161 packageLock, yarnLock, err := exec.checkIfLockFilesExist()162 if assert.NoError(t, err) {163 assert.True(t, packageLock)164 assert.True(t, yarnLock)165 }166 })167 t.Run("check that yarn.lock and package-lock do not exist", func(t *testing.T) {168 utils := newNpmMockUtilsBundle()169 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))170 options := ExecutorOptions{}171 exec := &Execute{172 Utils: &utils,173 Options: options,174 }175 packageLock, yarnLock, err := exec.checkIfLockFilesExist()176 if assert.NoError(t, err) {177 assert.False(t, packageLock)178 assert.False(t, yarnLock)179 }180 })181 t.Run("check Execute script", func(t *testing.T) {182 utils := newNpmMockUtilsBundle()183 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))184 options := ExecutorOptions{}185 exec := &Execute{186 Utils: &utils,187 Options: options,188 }189 err := exec.executeScript("package.json", "ci-lint", []string{"--silent"}, []string{"--tag", "tag1"})190 if assert.NoError(t, err) {191 if assert.Equal(t, 2, len(utils.execRunner.Calls)) {192 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-lint", "--silent", "--", "--tag", "tag1"}}, utils.execRunner.Calls[1])193 }194 }195 })196 t.Run("check Execute all scripts", func(t *testing.T) {197 utils := newNpmMockUtilsBundle()198 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))199 utils.AddFile(filepath.Join("src", "package.json"), []byte("{\"scripts\": { \"ci-build\": \"exit 0\" } }"))200 options := ExecutorOptions{}201 runScripts := []string{"ci-lint", "ci-build"}202 exec := &Execute{203 Utils: &utils,204 Options: options,205 }206 err := exec.RunScriptsInAllPackages(runScripts, nil, nil, false, nil)207 if assert.NoError(t, err) {208 if assert.Equal(t, 4, len(utils.execRunner.Calls)) {209 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-lint"}}, utils.execRunner.Calls[1])210 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-build"}}, utils.execRunner.Calls[3])211 }212 }213 })214 t.Run("check set npm registry", func(t *testing.T) {215 utils := newNpmMockUtilsBundle()216 utils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"exit 0\" } }"))217 utils.AddFile(filepath.Join("src", "package.json"), []byte("{\"scripts\": { \"ci-build\": \"exit 0\" } }"))218 utils.execRunner = &mock.ExecMockRunner{StdoutReturn: map[string]string{"npm config get registry": "undefined"}}219 options := ExecutorOptions{}220 options.DefaultNpmRegistry = "https://example.org/npm"221 exec := &Execute{222 Utils: &utils,223 Options: options,224 }225 err := exec.SetNpmRegistries()226 if assert.NoError(t, err) {227 if assert.Equal(t, 2, len(utils.execRunner.Calls)) {228 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"config", "get", "registry"}}, utils.execRunner.Calls[0])229 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"config", "set", "registry", exec.Options.DefaultNpmRegistry}}, utils.execRunner.Calls[1])230 }231 }232 })233 t.Run("Call run-scripts with virtual frame buffer", func(t *testing.T) {234 utils := newNpmMockUtilsBundle()235 utils.AddFile("package.json", []byte("{\"scripts\": { \"foo\": \"\" } }"))236 options := ExecutorOptions{}237 exec := &Execute{238 Utils: &utils,239 Options: options,240 }241 err := exec.RunScriptsInAllPackages([]string{"foo"}, nil, nil, true, nil)242 assert.Contains(t, utils.execRunner.Env, "DISPLAY=:99")243 assert.NoError(t, err)244 if assert.Len(t, utils.execRunner.Calls, 3) {245 xvfbCall := utils.execRunner.Calls[0]246 assert.Equal(t, "Xvfb", xvfbCall.Exec)247 assert.Equal(t, []string{"-ac", ":99", "-screen", "0", "1280x1024x16"}, xvfbCall.Params)248 assert.True(t, xvfbCall.Async)249 assert.True(t, xvfbCall.Execution.Killed)250 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "foo"}}, utils.execRunner.Calls[2])251 }252 })253}...
npmExecuteLint_test.go
Source:npmExecuteLint_test.go
...9 "testing"10)11type mockLintUtilsBundle struct {12 *mock.FilesMock13 execRunner *mock.ExecMockRunner14}15func (u *mockLintUtilsBundle) getExecRunner() command.ExecRunner {16 return u.execRunner17}18func (u *mockLintUtilsBundle) getGeneralPurposeConfig(configURL string) {19 u.AddFile(filepath.Join(".pipeline", ".eslintrc.json"), []byte(`abc`))20}21func newLintMockUtilsBundle() mockLintUtilsBundle {22 utils := mockLintUtilsBundle{FilesMock: &mock.FilesMock{}, execRunner: &mock.ExecMockRunner{}}23 return utils24}25func TestNpmExecuteLint(t *testing.T) {26 t.Run("Call with ci-lint script and one package.json", func(t *testing.T) {27 lintUtils := newLintMockUtilsBundle()28 lintUtils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"\" } }"))29 npmUtils := newNpmMockUtilsBundle()30 npmUtils.execRunner = lintUtils.execRunner31 npmUtils.FilesMock = lintUtils.FilesMock32 config := npmExecuteLintOptions{33 FailOnError: true,34 }35 npmExecutor := npmExecutorMock{utils: npmUtils, config: npmConfig{runScripts: []string{"ci-lint"}, runOptions: []string{"--silent"}}}36 err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)37 assert.NoError(t, err)38 })39 t.Run("Call default with ESLint config from user", func(t *testing.T) {40 lintUtils := newLintMockUtilsBundle()41 lintUtils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))42 lintUtils.AddFile(".eslintrc.json", []byte("{\"name\": \"Test\" }"))43 config := npmExecuteLintOptions{}44 config.DefaultNpmRegistry = "foo.bar"45 npmUtils := newNpmMockUtilsBundle()46 npmUtils.execRunner = lintUtils.execRunner47 npmExecutor := npm.Execute{Utils: &npmUtils, Options: npm.ExecutorOptions{}}48 err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)49 if assert.NoError(t, err) {50 if assert.Equal(t, 2, len(lintUtils.execRunner.Calls)) {51 assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"eslint", ".", "-f", "checkstyle", "-o", "./0_defaultlint.xml", "--ignore-pattern", "node_modules/", "--ignore-pattern", ".eslintrc.js"}}, lintUtils.execRunner.Calls[1])52 }53 }54 })55 t.Run("Call default with two ESLint configs from user", func(t *testing.T) {56 lintUtils := newLintMockUtilsBundle()57 lintUtils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))58 lintUtils.AddFile(".eslintrc.json", []byte("{\"name\": \"Test\" }"))59 lintUtils.AddFile(filepath.Join("src", ".eslintrc.json"), []byte("{\"name\": \"Test\" }"))60 config := npmExecuteLintOptions{}61 config.DefaultNpmRegistry = "foo.bar"62 npmUtils := newNpmMockUtilsBundle()63 npmUtils.execRunner = lintUtils.execRunner64 npmExecutor := npm.Execute{Utils: &npmUtils, Options: npm.ExecutorOptions{}}65 err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)66 if assert.NoError(t, err) {67 if assert.Equal(t, 3, len(lintUtils.execRunner.Calls)) {68 assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"eslint", ".", "-f", "checkstyle", "-o", "./0_defaultlint.xml", "--ignore-pattern", "node_modules/", "--ignore-pattern", ".eslintrc.js"}}, lintUtils.execRunner.Calls[1])69 assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"eslint", "src/**/*.js", "-f", "checkstyle", "-o", "./1_defaultlint.xml", "--ignore-pattern", "node_modules/", "--ignore-pattern", ".eslintrc.js"}}, lintUtils.execRunner.Calls[2])70 }71 }72 })73 t.Run("Default without ESLint config", func(t *testing.T) {74 lintUtils := newLintMockUtilsBundle()75 lintUtils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))76 config := npmExecuteLintOptions{}77 config.DefaultNpmRegistry = "foo.bar"78 npmUtils := newNpmMockUtilsBundle()79 npmUtils.execRunner = lintUtils.execRunner80 npmExecutor := npm.Execute{Utils: &npmUtils, Options: npm.ExecutorOptions{}}81 err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)82 if assert.NoError(t, err) {83 if assert.Equal(t, 3, len(lintUtils.execRunner.Calls)) {84 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"install", "eslint@^7.0.0", "typescript@^3.7.4", "@typescript-eslint/parser@^3.0.0", "@typescript-eslint/eslint-plugin@^3.0.0"}}, lintUtils.execRunner.Calls[1])85 assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"--no-install", "eslint", ".", "--ext", ".js,.jsx,.ts,.tsx", "-c", ".pipeline/.eslintrc.json", "-f", "checkstyle", "-o", "./defaultlint.xml", "--ignore-pattern", ".eslintrc.js"}}, lintUtils.execRunner.Calls[2])86 }87 }88 })89 t.Run("Call with ci-lint script and failOnError", func(t *testing.T) {90 lintUtils := newLintMockUtilsBundle()91 lintUtils.AddFile("package.json", []byte("{\"scripts\": { \"ci-lint\": \"\" } }"))92 lintUtils.execRunner = &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"npm run ci-lint --silent": errors.New("exit 1")}}93 config := npmExecuteLintOptions{}94 config.FailOnError = true95 config.DefaultNpmRegistry = "foo.bar"96 npmUtils := newNpmMockUtilsBundle()97 npmUtils.execRunner = lintUtils.execRunner98 npmUtils.FilesMock = lintUtils.FilesMock99 npmExecutor := npm.Execute{Utils: &npmUtils, Options: npm.ExecutorOptions{}}100 err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)101 if assert.EqualError(t, err, "ci-lint script execution failed with error: failed to run npm script ci-lint: exit 1. This might be the result of severe linting findings, or some other issue while executing the script. Please examine the linting results in the UI, the cilint.xml file, if available, or the log above. ") {102 if assert.Equal(t, 2, len(lintUtils.execRunner.Calls)) {103 assert.Equal(t, mock.ExecCall{Exec: "npm", Params: []string{"run", "ci-lint", "--silent"}}, lintUtils.execRunner.Calls[1])104 }105 }106 })107 t.Run("Call default with ESLint config from user and failOnError", func(t *testing.T) {108 lintUtils := newLintMockUtilsBundle()109 lintUtils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))110 lintUtils.AddFile(".eslintrc.json", []byte("{\"name\": \"Test\" }"))111 lintUtils.execRunner = &mock.ExecMockRunner{ShouldFailOnCommand: map[string]error{"eslint . -f checkstyle -o ./0_defaultlint.xml --ignore-pattern node_modules/ --ignore-pattern .eslintrc.js": errors.New("exit 1")}}112 config := npmExecuteLintOptions{}113 config.FailOnError = true114 config.DefaultNpmRegistry = "foo.bar"115 npmUtils := newNpmMockUtilsBundle()116 npmUtils.execRunner = lintUtils.execRunner117 npmExecutor := npm.Execute{Utils: &npmUtils, Options: npm.ExecutorOptions{}}118 err := runNpmExecuteLint(&npmExecutor, &lintUtils, &config)119 if assert.EqualError(t, err, "Lint execution failed. This might be the result of severe linting findings, problems with the provided ESLint configuration (.eslintrc.json), or another issue. Please examine the linting results in the UI or in 0_defaultlint.xml, if available, or the log above. ") {120 if assert.Equal(t, 2, len(lintUtils.execRunner.Calls)) {121 assert.Equal(t, mock.ExecCall{Exec: "npx", Params: []string{"eslint", ".", "-f", "checkstyle", "-o", "./0_defaultlint.xml", "--ignore-pattern", "node_modules/", "--ignore-pattern", ".eslintrc.js"}}, lintUtils.execRunner.Calls[1])122 }123 }124 })125 t.Run("Find ESLint configs", func(t *testing.T) {126 lintUtils := newLintMockUtilsBundle()127 lintUtils.AddFile("package.json", []byte("{\"name\": \"Test\" }"))128 lintUtils.AddFile(".eslintrc.json", []byte("{\"name\": \"Test\" }"))129 lintUtils.AddFile("src/.eslintrc.json", []byte("{\"name\": \"Test\" }"))130 lintUtils.AddFile("node_modules/.eslintrc.json", []byte("{\"name\": \"Test\" }")) // should be filtered out131 lintUtils.AddFile(".pipeline/.eslintrc.json", []byte("{\"name\": \"Test\" }")) // should be filtered out132 eslintConfigs := findEslintConfigs(&lintUtils)133 if assert.Equal(t, 2, len(eslintConfigs)) {134 assert.Contains(t, eslintConfigs, ".eslintrc.json")135 assert.Contains(t, eslintConfigs, filepath.Join("src", ".eslintrc.json"))...
Exec
Using AI Code Generation
1func main() {2 utils.Exec()3}4import (5func Exec() {6 fmt.Println(time.Now())7}8 /usr/local/go/src/utils (from $GOROOT)9 /go/src/utils (from $GOPATH)10 /usr/local/go/src/github.com/julienschmidt/httprouter (from $GOROOT)11 /go/src/github.com/julienschmidt/httprouter (from $GOPATH)
Exec
Using AI Code Generation
1import (2func main() {3 utils.Exec()4 fmt.Println("Hello World!")5}6import (7func Exec() {8 fmt.Println("Hello World!")9}
Exec
Using AI Code Generation
1import (2func main() {3 args := strings.Split(os.Args[2], " ")4 timeoutInt, err := strconv.Atoi(timeout)5 if err != nil {6 fmt.Println("Invalid timeout value")7 }8 timeoutDuration := time.Duration(timeoutInt) * time.Second9 status, err := utils.Exec(command, args, timeoutDuration)10 if err != nil {11 fmt.Println("Error executing command")12 }13 if status {14 fmt.Println("Command executed successfully")15 } else {16 fmt.Println("Command timed out")17 }18}19import (20func Exec(command string, args []string, timeout time.Duration) (bool, error) {21 cmd := exec.Command(command, args...)22 err := cmd.Start()23 if err != nil {24 }25 done := make(chan error, 1)26 go func() {27 done <- cmd.Wait()28 }()29 select {30 case <-time.After(timeout):31 if err := cmd.Process.Kill(); err != nil {32 }33 if err != nil {34 }35 }36}
Exec
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 utils.Exec()5}6import (7func Exec() {8 fmt.Println("Hello World")9}
Exec
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4 utils.Exec("ls -l")5}6import (7func Exec(cmd string) {8 fmt.Println("Executing command ", cmd)9 out, err := exec.Command(cmd).Output()10 if err != nil {11 fmt.Println(err)12 }13 fmt.Println(string(out))14}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!