From 6c0d1747d3c4f64679e3c845dc6b2650a5aa8dc4 Mon Sep 17 00:00:00 2001 From: Hariharan Subramanian Date: Thu, 8 Jun 2023 22:03:24 +0000 Subject: [PATCH] func for handling multiple separators --- cmd/create.go | 12 ++++++------ cmd/create_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index 05587f4e..a03d94b0 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -445,30 +445,30 @@ func (cc *createCmd) detectDefaults(detectedLang *config.DraftConfig, lowerLang defer f.Close() detectedDefaults := make([]config.BuilderVarDefault, 0) scanner := bufio.NewScanner(f) + separator := func(c rune) bool { return c == ' ' || c == '=' } + cutset := func(c rune) bool { return c == '\'' || c == '"' } for scanner.Scan() { line := scanner.Text() if strings.Contains(line, "sourceCompatibility") { - detectedVersion := strings.Split(line, " = ")[1] // sourceCompatibility = '1.8' - detectedVersion = strings.Trim(detectedVersion, "'") + detectedVersion := strings.FieldsFunc(line, separator)[1] // sourceCompatibility = '1.8' + detectedVersion = strings.TrimFunc(detectedVersion, cutset) detectedVersion = detectedVersion + "-jre" builderVarDefault := config.BuilderVarDefault{ Name: "VERSION", Value: detectedVersion, } detectedDefaults = append(detectedDefaults, builderVarDefault) - //log.Info("Detected VERSION is %q from build.gradle for %s project", detectedVersion, lowerLang) } if strings.Contains(line, "targetCompatibility") { - detectedBuilderVersion := strings.Split(line, " = ")[1] // targetCompatibility = '1.8' - detectedBuilderVersion = strings.Trim(detectedBuilderVersion, "'") + detectedBuilderVersion := strings.FieldsFunc(line, separator)[1] // targetCompatibility = '1.8' + detectedBuilderVersion = strings.TrimFunc(detectedBuilderVersion, cutset) detectedBuilderVersion = "jdk" + detectedBuilderVersion detectedBuilderVar := config.BuilderVarDefault{ Name: "BUILDERVERSION", Value: detectedBuilderVersion, } detectedDefaults = append(detectedDefaults, detectedBuilderVar) - //log.Info("Detected BUILDER VERSION is %s from build.gradle for %s project", detectedBuilderVersion[0], lowerLang) } detectedLang.DetectedDefaults = detectedDefaults } diff --git a/cmd/create_test.go b/cmd/create_test.go index b1cfa726..6c7e318c 100644 --- a/cmd/create_test.go +++ b/cmd/create_test.go @@ -269,3 +269,16 @@ func TestDetectDefaultsNoTargetComptability(t *testing.T) { assert.Equal(t, "11-jre", mockDraftConfig.DetectedDefaults[0].Value) os.Remove("build.gradle") } + +func TestDetectDefaultsWithoutSpacesInDoubleQuotes(t *testing.T) { + ioutil.WriteFile("build.gradle", []byte("sourceCompatibility=\"17\"\ntargetCompatibility = '12'"), 0644) + mockCC := &createCmd{} + mockDraftConfig := &config.DraftConfig{} + mockCC.detectDefaults(mockDraftConfig, "gradle") + assert.Equal(t, 2, len(mockDraftConfig.DetectedDefaults)) + assert.Equal(t, "VERSION", mockDraftConfig.DetectedDefaults[0].Name) + assert.Equal(t, "17-jre", mockDraftConfig.DetectedDefaults[0].Value) + assert.Equal(t, "BUILDERVERSION", mockDraftConfig.DetectedDefaults[1].Name) + assert.Equal(t, "jdk12", mockDraftConfig.DetectedDefaults[1].Value) + os.Remove("build.gradle") +}