From e11c508ae64139c2f240c68635fc7e086cbfc353 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 10 Sep 2019 16:59:58 +0200 Subject: [PATCH 1/2] Removed usueless extra unquoting --- main.go | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 79d439c2..fd349cf2 100644 --- a/main.go +++ b/main.go @@ -245,9 +245,8 @@ func main() { } // FLAG_HARDWARE - if hardwareFolders, err := toSliceOfUnquoted(hardwareFoldersFlag); err != nil { - printCompleteError(err) - } else if len(hardwareFolders) > 0 { + hardwareFolders := toSliceOfUnquoted(hardwareFoldersFlag) + if len(hardwareFolders) > 0 { ctx.HardwareDirs = paths.NewPathList(hardwareFolders...) } if len(ctx.HardwareDirs) == 0 { @@ -255,9 +254,8 @@ func main() { } // FLAG_TOOLS - if toolsFolders, err := toSliceOfUnquoted(toolsFoldersFlag); err != nil { - printCompleteError(err) - } else if len(toolsFolders) > 0 { + toolsFolders := toSliceOfUnquoted(toolsFoldersFlag) + if len(toolsFolders) > 0 { ctx.ToolsDirs = paths.NewPathList(toolsFolders...) } if len(ctx.ToolsDirs) == 0 { @@ -265,23 +263,20 @@ func main() { } // FLAG_LIBRARIES - if librariesFolders, err := toSliceOfUnquoted(librariesFoldersFlag); err != nil { - printCompleteError(err) - } else if len(librariesFolders) > 0 { + librariesFolders := toSliceOfUnquoted(librariesFoldersFlag) + if len(librariesFolders) > 0 { ctx.OtherLibrariesDirs = paths.NewPathList(librariesFolders...) } // FLAG_BUILT_IN_LIBRARIES - if librariesBuiltInFolders, err := toSliceOfUnquoted(librariesBuiltInFoldersFlag); err != nil { - printCompleteError(err) - } else if len(librariesBuiltInFolders) > 0 { + librariesBuiltInFolders := toSliceOfUnquoted(librariesBuiltInFoldersFlag) + if len(librariesBuiltInFolders) > 0 { ctx.BuiltInLibrariesDirs = paths.NewPathList(librariesBuiltInFolders...) } // FLAG_PREFS - if customBuildProperties, err := toSliceOfUnquoted(customBuildPropertiesFlag); err != nil { - printCompleteError(err) - } else if len(customBuildProperties) > 0 { + customBuildProperties := toSliceOfUnquoted(customBuildPropertiesFlag) + if len(customBuildProperties) > 0 { ctx.CustomBuildProperties = customBuildProperties } @@ -420,16 +415,23 @@ func toExitCode(err error) int { return 1 } -func toSliceOfUnquoted(value []string) ([]string, error) { +func toSliceOfUnquoted(value []string) []string { var values []string for _, v := range value { - v, err := gohasissues.Unquote(v) - if err != nil { - return nil, err - } - values = append(values, v) + values = append(values, unquote(v)) + } + return values +} + +func unquote(s string) string { + if stringStartsEndsWith(s, "'") || stringStartsEndsWith(s, "\"") { + s = s[1 : len(s)-1] } - return values, nil + return s +} + +func stringStartsEndsWith(s string, c string) bool { + return strings.HasPrefix(s, c) && strings.HasSuffix(s, c) } func printCompleteError(err error) { From 028688f5f13ee2e6cf12715fc7a6adde51880fe5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 10 Sep 2019 17:15:17 +0200 Subject: [PATCH 2/2] Added quoting test --- go.mod | 1 + unquote_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 unquote_test.go diff --git a/go.mod b/go.mod index c728979b..e6255468 100644 --- a/go.mod +++ b/go.mod @@ -9,4 +9,5 @@ require ( github.com/go-errors/errors v1.0.1 github.com/juju/errors v0.0.0-20190207033735-e65537c515d7 // indirect github.com/sirupsen/logrus v1.4.2 + github.com/stretchr/testify v1.3.0 ) diff --git a/unquote_test.go b/unquote_test.go new file mode 100644 index 00000000..cc3e227f --- /dev/null +++ b/unquote_test.go @@ -0,0 +1,43 @@ +/* + * This file is part of Arduino Builder. + * + * Arduino Builder is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + */ + +package main + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestStrConvUnquote(t *testing.T) { + require.Equal(t, "ciao", unquote("ciao")) + require.Equal(t, "arduino:avr:uno", unquote("arduino:avr:uno")) + require.Equal(t, "arduino:avr:uno", unquote("\"arduino:avr:uno\"")) + require.Equal(t, "arduino:avr:uno", unquote("'arduino:avr:uno'")) +}