From 0489aaec772d69445ba4be66d2c5f003ad029c16 Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Wed, 10 Aug 2022 11:33:49 +0200 Subject: [PATCH 1/4] Migrated TestHelp from test_main.py to main_test.go --- internal/integrationtest/main/main_test.go | 41 ++++++++++++++++++++++ test/test_main.py | 7 ---- 2 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 internal/integrationtest/main/main_test.go diff --git a/internal/integrationtest/main/main_test.go b/internal/integrationtest/main/main_test.go new file mode 100644 index 00000000000..295df4fe756 --- /dev/null +++ b/internal/integrationtest/main/main_test.go @@ -0,0 +1,41 @@ +// This file is part of arduino-cli. +// +// Copyright 2022 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package main_test + +import ( + "testing" + + "github.com/arduino/arduino-cli/internal/integrationtest" + "github.com/arduino/go-paths-helper" + "github.com/stretchr/testify/require" + "go.bug.st/testsuite" +) + +func TestHelp(t *testing.T) { + env := testsuite.NewEnvironment(t) + defer env.CleanUp() + + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), + UseSharedStagingFolder: true, + }) + + // Run help and check the output message + stdout, stderr, err := cli.Run("help") + require.NoError(t, err) + require.Empty(t, stderr) + require.Contains(t, string(stdout), "Usage") +} diff --git a/test/test_main.py b/test/test_main.py index 91cddc57732..4774cd5eb59 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -19,13 +19,6 @@ import yaml -def test_help(run_command): - result = run_command(["help"]) - assert result.ok - assert result.stderr == "" - assert "Usage" in result.stdout - - def test_version(run_command): result = run_command(["version"]) assert result.ok From cda7151d7038bf16c9c2e96569e4637a51fdac57 Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Thu, 11 Aug 2022 10:38:59 +0200 Subject: [PATCH 2/4] Migrated TestVersion from test_main.py to main_test.go --- internal/integrationtest/main/main_test.go | 43 ++++++++++++++++++++++ test/test_main.py | 17 --------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/internal/integrationtest/main/main_test.go b/internal/integrationtest/main/main_test.go index 295df4fe756..4f85c5ec710 100644 --- a/internal/integrationtest/main/main_test.go +++ b/internal/integrationtest/main/main_test.go @@ -16,11 +16,13 @@ package main_test import ( + "encoding/json" "testing" "github.com/arduino/arduino-cli/internal/integrationtest" "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" + semver "go.bug.st/relaxed-semver" "go.bug.st/testsuite" ) @@ -39,3 +41,44 @@ func TestHelp(t *testing.T) { require.Empty(t, stderr) require.Contains(t, string(stdout), "Usage") } + +func TestVersion(t *testing.T) { + env := testsuite.NewEnvironment(t) + defer env.CleanUp() + + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), + UseSharedStagingFolder: true, + }) + + // Run version and check the output message + stdout, stderr, err := cli.Run("version") + require.NoError(t, err) + require.Contains(t, string(stdout), "Version:") + require.Contains(t, string(stdout), "Commit:") + require.Empty(t, stderr) + + // Checks if "version --format json" has a json as an output + stdout, _, err = cli.Run("version", "--format", "json") + require.NoError(t, err) + var jsonMap map[string]string + err = json.Unmarshal(stdout, &jsonMap) + require.NoError(t, err) + + // Checks if Application's value is arduino-cli + require.Equal(t, jsonMap["Application"], "arduino-cli") + + // Checks if VersionString's value is git-snapshot, nightly or a valid semantic versioning + switch version := jsonMap["VersionString"]; version { + case "git-snapshot": + require.Contains(t, version, "git-snapshot") + case "nigthly": + require.Contains(t, version, "nightly") + default: + _, err = semver.Parse(version) + require.NoError(t, err) + } + + // Checks if Commit's value is not empty + require.NotEmpty(t, jsonMap["Commit"]) +} diff --git a/test/test_main.py b/test/test_main.py index 4774cd5eb59..e93fd48db0d 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -18,23 +18,6 @@ import semver import yaml - -def test_version(run_command): - result = run_command(["version"]) - assert result.ok - assert "Version:" in result.stdout - assert "Commit:" in result.stdout - assert "" == result.stderr - - result = run_command(["version", "--format", "json"]) - assert result.ok - parsed_out = json.loads(result.stdout) - assert parsed_out.get("Application", False) == "arduino-cli" - version = parsed_out.get("VersionString", False) - assert semver.VersionInfo.isvalid(version=version) or "git-snapshot" in version or "nightly" in version - assert isinstance(parsed_out.get("Commit", False), str) - - def test_log_options(run_command, data_dir): """ using `version` as a test command From 2330ca30b6634a5717eaccd540acbbef7426b571 Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Thu, 11 Aug 2022 16:32:58 +0200 Subject: [PATCH 3/4] Migrated TestInventoryCreation from test_main.py to main_test.go --- internal/integrationtest/main/main_test.go | 24 ++++++++++++++++++++++ test/test_main.py | 16 --------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/internal/integrationtest/main/main_test.go b/internal/integrationtest/main/main_test.go index 4f85c5ec710..975b45c8df7 100644 --- a/internal/integrationtest/main/main_test.go +++ b/internal/integrationtest/main/main_test.go @@ -17,6 +17,7 @@ package main_test import ( "encoding/json" + "strings" "testing" "github.com/arduino/arduino-cli/internal/integrationtest" @@ -82,3 +83,26 @@ func TestVersion(t *testing.T) { // Checks if Commit's value is not empty require.NotEmpty(t, jsonMap["Commit"]) } + +func TestInventoryCreation(t *testing.T) { + // Using version as a test command + + env := testsuite.NewEnvironment(t) + defer env.CleanUp() + + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), + UseSharedStagingFolder: true, + }) + + // no logs + stdout, _, _ := cli.Run("version") + line := strings.TrimSpace(string(stdout)) + outLines := strings.Split(line, "\n") + require.Len(t, outLines, 1) + + // parse inventory file + inventoryFile := cli.DataDir().Join("inventory.yaml") + stream, _ := inventoryFile.ReadFile() + require.True(t, strings.Contains(string(stream), "installation")) +} diff --git a/test/test_main.py b/test/test_main.py index e93fd48db0d..23808178691 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -50,19 +50,3 @@ def test_log_options(run_command, data_dir): with open(log_file) as f: for line in f.readlines(): json.loads(line) - - -def test_inventory_creation(run_command, data_dir): - """ - using `version` as a test command - """ - - # no logs - out_lines = run_command(["version"]).stdout.strip().split("\n") - assert len(out_lines) == 1 - - # parse inventory file - inventory_file = os.path.join(data_dir, "inventory.yaml") - with open(inventory_file, "r") as stream: - inventory = yaml.safe_load(stream) - assert "installation" in inventory From 8a86752193fdf2cae07aeb77d0a648433253fa7f Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Thu, 11 Aug 2022 17:55:23 +0200 Subject: [PATCH 4/4] Migrated TestLogOptions to main_test.go and deleted test_main.py --- internal/integrationtest/main/main_test.go | 64 +++++++++++++++++++++- test/test_main.py | 52 ------------------ 2 files changed, 61 insertions(+), 55 deletions(-) delete mode 100644 test/test_main.py diff --git a/internal/integrationtest/main/main_test.go b/internal/integrationtest/main/main_test.go index 975b45c8df7..940118410ff 100644 --- a/internal/integrationtest/main/main_test.go +++ b/internal/integrationtest/main/main_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/require" semver "go.bug.st/relaxed-semver" "go.bug.st/testsuite" + "go.bug.st/testsuite/requirejson" ) func TestHelp(t *testing.T) { @@ -84,9 +85,64 @@ func TestVersion(t *testing.T) { require.NotEmpty(t, jsonMap["Commit"]) } -func TestInventoryCreation(t *testing.T) { +func TestLogOptions(t *testing.T) { // Using version as a test command + env := testsuite.NewEnvironment(t) + defer env.CleanUp() + cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ + ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), + UseSharedStagingFolder: true, + }) + + // No logs + stdout, _, err := cli.Run("version") + require.NoError(t, err) + trimOut := strings.TrimSpace(string(stdout)) + outLines := strings.Split(trimOut, "\n") + require.Len(t, outLines, 1) + + // Plain text logs on stdout + stdout, _, err = cli.Run("version", "-v") + require.NoError(t, err) + trimOut = strings.TrimSpace(string(stdout)) + outLines = strings.Split(trimOut, "\n") + require.Greater(t, len(outLines), 1) + require.True(t, strings.HasPrefix(outLines[0], "\x1b[36mINFO\x1b[0m")) // account for the colors + + // Plain text logs on file + logFile := cli.DataDir().Join("log.txt") + _, _, err = cli.Run("version", "--log-file", logFile.String()) + require.NoError(t, err) + lines, _ := logFile.ReadFileAsLines() + require.True(t, strings.HasPrefix(lines[0], "time=\"")) + + // json on stdout + stdout, _, err = cli.Run("version", "-v", "--log-format", "JSON") + require.NoError(t, err) + trimOut = strings.TrimSpace(string(stdout)) + outLines = strings.Split(trimOut, "\n") + requirejson.Contains(t, []byte(outLines[0]), `{ "level" }`) + + // Check if log.json contains readable json in each line + var v interface{} + logFileJson := cli.DataDir().Join("log.json") + _, _, err = cli.Run("version", "--log-format", "JSON", "--log-file", logFileJson.String()) + require.NoError(t, err) + fileContent, err := logFileJson.ReadFileAsLines() + require.NoError(t, err) + for _, line := range fileContent { + // exclude empty lines since they are not valid json + if line == "" { + continue + } + err = json.Unmarshal([]byte(line), &v) + require.NoError(t, err) + } +} + +func TestInventoryCreation(t *testing.T) { + // Using version as a test command env := testsuite.NewEnvironment(t) defer env.CleanUp() @@ -96,13 +152,15 @@ func TestInventoryCreation(t *testing.T) { }) // no logs - stdout, _, _ := cli.Run("version") + stdout, _, err := cli.Run("version") + require.NoError(t, err) line := strings.TrimSpace(string(stdout)) outLines := strings.Split(line, "\n") require.Len(t, outLines, 1) // parse inventory file inventoryFile := cli.DataDir().Join("inventory.yaml") - stream, _ := inventoryFile.ReadFile() + stream, err := inventoryFile.ReadFile() + require.NoError(t, err) require.True(t, strings.Contains(string(stream), "installation")) } diff --git a/test/test_main.py b/test/test_main.py deleted file mode 100644 index 23808178691..00000000000 --- a/test/test_main.py +++ /dev/null @@ -1,52 +0,0 @@ -# This file is part of arduino-cli. -# -# Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -# -# This software is released under the GNU General Public License version 3, -# which covers the main part of arduino-cli. -# The terms of this license can be found at: -# https://www.gnu.org/licenses/gpl-3.0.en.html -# -# You can be released from the requirements of the above licenses by purchasing -# a commercial license. Buying such a license is mandatory if you want to modify or -# otherwise use the software for commercial activities involving the Arduino -# software without disclosing the source code of your own applications. To purchase -# a commercial license, send an email to license@arduino.cc. -import json -import os - -import semver -import yaml - -def test_log_options(run_command, data_dir): - """ - using `version` as a test command - """ - - # no logs - out_lines = run_command(["version"]).stdout.strip().split("\n") - assert len(out_lines) == 1 - - # plain text logs on stdoud - out_lines = run_command(["version", "-v"]).stdout.strip().split("\n") - assert len(out_lines) > 1 - assert out_lines[0].startswith("\x1b[36mINFO\x1b[0m") # account for the colors - - # plain text logs on file - log_file = os.path.join(data_dir, "log.txt") - run_command(["version", "--log-file", log_file]) - with open(log_file) as f: - lines = f.readlines() - assert lines[0].startswith('time="') # file format is different from console - - # json on stdout - out_lines = run_command(["version", "-v", "--log-format", "JSON"]).stdout.strip().split("\n") - lg = json.loads(out_lines[0]) - assert "level" in lg - - # json on file - log_file = os.path.join(data_dir, "log.json") - run_command(["version", "--log-format", "json", "--log-file", log_file]) - with open(log_file) as f: - for line in f.readlines(): - json.loads(line)