Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command sketch new do not join sketchbook path to sketch name if only sketchname is provided #396

Merged
merged 16 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ __pycache__
venv
.pytest_cache
/dist
/.pytest-tmp-dir

# gRPC client example folder
/client_example/client_example
Expand Down
10 changes: 2 additions & 8 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,6 @@ func TestUploadIntegration(t *testing.T) {
require.NotZero(t, exitCode)
}

func TestSketchCommandsIntegration(t *testing.T) {
exitCode, d := executeWithArgs("sketch", "new", "Test")
require.Zero(t, exitCode)
require.Contains(t, string(d), "Sketch created")
}

func TestCompileCommandsIntegration(t *testing.T) {
// Set staging dir to a temporary dir
tmp := tmpDirOrDie()
Expand All @@ -319,11 +313,11 @@ func TestCompileCommandsIntegration(t *testing.T) {
require.Zero(t, exitCode)

// Create a test sketch
exitCode, d := executeWithArgs("sketch", "new", "Test1")
test1 := filepath.Join(currSketchbookDir, "Test1")
exitCode, d := executeWithArgs("sketch", "new", test1)
require.Zero(t, exitCode)

// Build sketch without FQBN
test1 := filepath.Join(currSketchbookDir, "Test1")
exitCode, d = executeWithArgs("compile", test1)
require.NotZero(t, exitCode)
require.Contains(t, string(d), "no FQBN provided")
Expand Down
22 changes: 15 additions & 7 deletions cli/sketch/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
package sketch

import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/spf13/cobra"
)

Expand All @@ -47,17 +49,23 @@ void loop() {
`)

func runNewCommand(cmd *cobra.Command, args []string) {
sketchDir := globals.Config.SketchbookDir.Join(args[0])
if err := sketchDir.MkdirAll(); err != nil {
// Trim to avoid issues if user creates a sketch adding the .ino extesion to the name
trimmedSketchName := strings.TrimSuffix(args[0], ".ino")
sketchDir, err := filepath.Abs(trimmedSketchName)
if err != nil {
feedback.Errorf("Error creating sketch: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
if err := os.MkdirAll(sketchDir, os.FileMode(0755)); err != nil {
feedback.Errorf("Could not create sketch directory: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

sketchFile := sketchDir.Join(args[0] + ".ino")
if err := sketchFile.WriteFile(emptySketch); err != nil {
sketchName := filepath.Base(sketchDir)
sketchFile := filepath.Join(sketchDir, sketchName+".ino")
masci marked this conversation as resolved.
Show resolved Hide resolved
if err := ioutil.WriteFile(sketchFile, emptySketch, os.FileMode(0644)); err != nil {
feedback.Errorf("Error creating sketch: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

feedback.Print("Sketch created in: " + sketchDir.String())
feedback.Print("Sketch created in: " + sketchDir)
}
18 changes: 15 additions & 3 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import os

import pytest
from invoke import run
from invoke.context import Context


@pytest.fixture(scope="function")
Expand All @@ -38,7 +38,17 @@ def downloads_dir(tmpdir_factory):


@pytest.fixture(scope="function")
def run_command(data_dir, downloads_dir):
def working_dir(tmpdir_factory):
"""
A tmp folder to work in
will be created before running each test and deleted
at the end, this way all the tests work in isolation.
"""
return str(tmpdir_factory.mktemp("ArduinoTestWork"))


@pytest.fixture(scope="function")
def run_command(data_dir, downloads_dir, working_dir):
"""
Provide a wrapper around invoke's `run` API so that every test
will work in the same temporary folder.
Expand All @@ -55,6 +65,8 @@ def run_command(data_dir, downloads_dir):

def _run(cmd_string):
cli_full_line = "{} {}".format(cli_path, cmd_string)
return run(cli_full_line, echo=False, hide=True, warn=True, env=env)
run_context = Context()
with run_context.cd(working_dir):
return run_context.run(cli_full_line, echo=False, hide=True, warn=True, env=env)

return _run
2 changes: 1 addition & 1 deletion test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ astroid==2.2.5
atomicwrites==1.3.0
attrs==19.1.0
importlib-metadata==0.18
invoke==1.2.0
invoke==1.3.0
isort==4.3.21
lazy-object-proxy==1.4.1
mccabe==0.6.1
Expand Down
4 changes: 2 additions & 2 deletions test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_compile_with_simple_sketch(run_command, data_dir):
fqbn = "arduino:avr:uno"

# Create a test sketch
result = run_command("sketch new {}".format(sketch_name))
result = run_command("sketch new {}".format(sketch_path))
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout

Expand Down Expand Up @@ -83,7 +83,7 @@ def test_compile_and_compile_combo(run_command, data_dir):
# Create a test sketch
sketch_name = "CompileAndUploadIntegrationTest"
sketch_path = os.path.join(data_dir, sketch_name)
result = run_command("sketch new CompileAndUploadIntegrationTest")
result = run_command("sketch new {}".format(sketch_path))
assert result.ok
assert "Sketch created in: {}".format(sketch_path) in result.stdout

Expand Down
58 changes: 58 additions & 0 deletions test/test_sketch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# This file is part of arduino-cli.
#
# Copyright 2019 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 [email protected].
import os
import platform

import pytest

from test.common import running_on_ci


@pytest.mark.skipif(running_on_ci() and platform.system() == "Windows",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The e2e testing in the Github CI windows VM is temporarly disabled for the sketch new feature.
We are experiencing an odd behavior for windows VM in the test pipeline that we are not able to reproduce on a physical windows machine. The issue seems to be related to the managing of pytest temporary directories.
The test will remain disabled until a root cause is found and the fix introduced as a separate PR.

reason="Test disabled on Github Actions Win VM until tmpdir inconsistent behavior bug is fixed")
def test_sketch_new(run_command, working_dir):
# Create a test sketch in current directory
current_path = working_dir
sketch_name = "SketchNewIntegrationTest"
current_sketch_path = os.path.join(current_path, sketch_name)
result = run_command("sketch new {}".format(sketch_name))
assert result.ok
assert "Sketch created in: {}".format(current_sketch_path) in result.stdout
assert os.path.isfile(os.path.join(current_sketch_path, sketch_name + ".ino"))

# Create a test sketch in current directory but using an absolute path
sketch_name = "SketchNewIntegrationTestAbsolute"
current_sketch_path = os.path.join(current_path, sketch_name)
result = run_command("sketch new {}".format(current_sketch_path))
assert result.ok
assert "Sketch created in: {}".format(current_sketch_path) in result.stdout
assert os.path.isfile(os.path.join(current_sketch_path, sketch_name + ".ino"))

# Create a test sketch in current directory subpath but using an absolute path
sketch_name = "SketchNewIntegrationTestSubpath"
sketch_subpath = os.path.join("subpath", sketch_name)
current_sketch_path = os.path.join(current_path, sketch_subpath)
result = run_command("sketch new {}".format(sketch_subpath))
assert result.ok
assert "Sketch created in: {}".format(current_sketch_path) in result.stdout
assert os.path.isfile(os.path.join(current_sketch_path, sketch_name + ".ino"))

# Create a test sketch in current directory using .ino extension
sketch_name = "SketchNewIntegrationTestDotIno"
current_sketch_path = os.path.join(current_path, sketch_name)
result = run_command("sketch new {}".format(sketch_name + ".ino"))
assert result.ok
assert "Sketch created in: {}".format(current_sketch_path) in result.stdout
assert os.path.isfile(os.path.join(current_sketch_path, sketch_name + ".ino"))