Skip to content

Commit 5dbeb05

Browse files
committed
[skip changelog] Fix tests on Windows
1 parent 3c000e1 commit 5dbeb05

File tree

4 files changed

+60
-37
lines changed

4 files changed

+60
-37
lines changed

Diff for: commands/instances.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/url"
2626
"os"
2727
"path"
28+
"path/filepath"
2829
"strings"
2930

3031
"github.com/arduino/arduino-cli/arduino/builder"
@@ -779,7 +780,7 @@ func ArchiveSketch(ctx context.Context, req *rpc.ArchiveSketchReq) (*rpc.Archive
779780
}
780781

781782
// Skips build folder
782-
if strings.HasPrefix(filePath.String(), sketchName+"/build") {
783+
if strings.HasPrefix(filePath.String(), sketchName+string(filepath.Separator)+"build") {
783784
continue
784785
}
785786
}

Diff for: test/conftest.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import platform
1717
import signal
18+
from pathlib import Path
1819

1920
import pytest
2021
import simplejson as json
@@ -84,20 +85,29 @@ def run_command(pytestconfig, data_dir, downloads_dir, working_dir):
8485
Useful reference:
8586
http://docs.pyinvoke.org/en/1.4/api/runners.html#invoke.runners.Result
8687
"""
87-
cli_path = os.path.join(str(pytestconfig.rootdir), "..", "arduino-cli")
88+
89+
cli_path = Path(pytestconfig.rootdir).parent / "arduino-cli"
8890
env = {
8991
"ARDUINO_DATA_DIR": data_dir,
9092
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
9193
"ARDUINO_SKETCHBOOK_DIR": data_dir,
9294
}
93-
os.makedirs(os.path.join(data_dir, "packages"))
95+
(Path(data_dir) / "packages").mkdir()
9496

9597
def _run(cmd_string, custom_working_dir=None):
9698
if not custom_working_dir:
9799
custom_working_dir = working_dir
98-
cli_full_line = "{} {}".format(cli_path, cmd_string)
100+
cli_full_line = '"{}" {}'.format(cli_path, cmd_string)
99101
run_context = Context()
100-
with run_context.cd(custom_working_dir):
102+
# It might happen that we need to change directories between drives on Windows,
103+
# in that case the "/d" flag must be used otherwise directory wouldn't change
104+
cd_command = "cd"
105+
if platform.system() == "Windows":
106+
cd_command += " /d"
107+
# Context.cd() is not used since it doesn't work correctly on Windows.
108+
# It escapes spaces in the path using "\ " but it doesn't always work,
109+
# wrapping the path in quotation marks is the safest approach
110+
with run_context.prefix(f'{cd_command} "{custom_working_dir}"'):
101111
return run_context.run(cli_full_line, echo=False, hide=True, warn=True, env=env)
102112

103113
return _run
@@ -114,15 +124,23 @@ def daemon_runner(pytestconfig, data_dir, downloads_dir, working_dir):
114124
http://docs.pyinvoke.org/en/1.4/api/runners.html#invoke.runners.Local
115125
http://docs.pyinvoke.org/en/1.4/api/runners.html
116126
"""
117-
cli_full_line = os.path.join(str(pytestconfig.rootdir), "..", "arduino-cli daemon")
127+
cli_full_line = str(Path(pytestconfig.rootdir).parent / "arduino-cli daemon")
118128
env = {
119129
"ARDUINO_DATA_DIR": data_dir,
120130
"ARDUINO_DOWNLOADS_DIR": downloads_dir,
121131
"ARDUINO_SKETCHBOOK_DIR": data_dir,
122132
}
123-
os.makedirs(os.path.join(data_dir, "packages"))
133+
(Path(data_dir) / "packages").mkdir()
124134
run_context = Context()
125-
run_context.cd(working_dir)
135+
# It might happen that we need to change directories between drives on Windows,
136+
# in that case the "/d" flag must be used otherwise directory wouldn't change
137+
cd_command = "cd"
138+
if platform.system() == "Windows":
139+
cd_command += " /d"
140+
# Context.cd() is not used since it doesn't work correctly on Windows.
141+
# It escapes spaces in the path using "\ " but it doesn't always work,
142+
# wrapping the path in quotation marks is the safest approach
143+
run_context.prefix(f'{cd_command} "{working_dir}"')
126144
# Local Class is the implementation of a Runner abstract class
127145
runner = Local(run_context)
128146
runner.run(cli_full_line, echo=False, hide=True, warn=True, env=env, asynchronous=True)

Diff for: test/test_archive.py

+30-26
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def test_archive_dot_arg_absolute_zip_path(run_command, copy_sketch, working_dir
109109
archives_folder = f"{working_dir}/my_archives/"
110110
Path(archives_folder).mkdir()
111111

112-
result = run_command(f"archive . {archives_folder}", copy_sketch)
112+
result = run_command(f'archive . "{archives_folder}"', copy_sketch)
113+
print(result.stderr)
113114
assert result.ok
114115

115116
archive = zipfile.ZipFile(f"{archives_folder}/sketch_simple.zip")
@@ -165,7 +166,7 @@ def test_archive_dot_arg_absolute_zip_path_and_name_without_extension(run_comman
165166
archives_folder = f"{working_dir}/my_archives/"
166167
Path(archives_folder).mkdir()
167168

168-
result = run_command(f"archive . {archives_folder}/my_custom_sketch", copy_sketch)
169+
result = run_command(f'archive . "{archives_folder}/my_custom_sketch"', copy_sketch)
169170
assert result.ok
170171

171172
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -193,7 +194,7 @@ def test_archive_dot_arg_custom_zip_path_and_name_with_extension(run_command, co
193194
archives_folder = f"{working_dir}/my_archives/"
194195
Path(archives_folder).mkdir()
195196

196-
result = run_command(f"archive . {archives_folder}/my_custom_sketch.zip", copy_sketch)
197+
result = run_command(f'archive . "{archives_folder}/my_custom_sketch.zip"', copy_sketch)
197198
assert result.ok
198199

199200
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -241,7 +242,7 @@ def test_archive_relative_sketch_path(run_command, copy_sketch, working_dir):
241242

242243

243244
def test_archive_absolute_sketch_path(run_command, copy_sketch, working_dir):
244-
result = run_command(f"archive {working_dir}/sketch_simple", copy_sketch)
245+
result = run_command(f'archive "{working_dir}/sketch_simple"', copy_sketch)
245246
assert result.ok
246247

247248
archive = zipfile.ZipFile(f"{working_dir}/sketch_simple.zip")
@@ -297,7 +298,7 @@ def test_archive_relative_sketch_path_with_absolute_zip_path(run_command, copy_s
297298
archives_folder = f"{working_dir}/my_archives/"
298299
Path(archives_folder).mkdir()
299300

300-
result = run_command(f"archive ./sketch_simple {archives_folder}")
301+
result = run_command(f'archive ./sketch_simple "{archives_folder}"')
301302
assert result.ok
302303

303304
archive = zipfile.ZipFile(f"{archives_folder}/sketch_simple.zip")
@@ -387,7 +388,7 @@ def test_archive_relative_sketch_path_with_absolute_zip_path_and_name_without_ex
387388
archives_folder = f"{working_dir}/my_archives/"
388389
Path(archives_folder).mkdir()
389390

390-
result = run_command(f"archive ./sketch_simple {archives_folder}/my_custom_sketch")
391+
result = run_command(f'archive ./sketch_simple "{archives_folder}/my_custom_sketch"')
391392
assert result.ok
392393

393394
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -417,7 +418,7 @@ def test_archive_relative_sketch_path_with_absolute_zip_path_and_name_with_exten
417418
archives_folder = f"{working_dir}/my_archives/"
418419
Path(archives_folder).mkdir()
419420

420-
result = run_command(f"archive ./sketch_simple {archives_folder}/my_custom_sketch.zip")
421+
result = run_command(f'archive ./sketch_simple "{archives_folder}/my_custom_sketch.zip"')
421422
assert result.ok
422423

423424
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -445,7 +446,7 @@ def test_archive_absolute_sketch_path_with_relative_zip_path(run_command, copy_s
445446
archives_folder = f"{working_dir}/my_archives/"
446447
Path(archives_folder).mkdir()
447448

448-
result = run_command(f"archive {working_dir}/sketch_simple ./my_archives")
449+
result = run_command(f'archive "{working_dir}/sketch_simple" ./my_archives')
449450
assert result.ok
450451

451452
archive = zipfile.ZipFile(f"{working_dir}/my_archives/sketch_simple.zip")
@@ -473,7 +474,7 @@ def test_archive_absolute_sketch_path_with_absolute_zip_path(run_command, copy_s
473474
archives_folder = f"{working_dir}/my_archives/"
474475
Path(archives_folder).mkdir()
475476

476-
result = run_command(f"archive {working_dir}/sketch_simple {archives_folder}", copy_sketch)
477+
result = run_command(f'archive "{working_dir}/sketch_simple" "{archives_folder}"', copy_sketch)
477478
assert result.ok
478479

479480
archive = zipfile.ZipFile(f"{archives_folder}/sketch_simple.zip")
@@ -503,7 +504,7 @@ def test_archive_absolute_sketch_path_with_relative_zip_path_and_name_without_ex
503504
archives_folder = f"{working_dir}/my_archives/"
504505
Path(archives_folder).mkdir()
505506

506-
result = run_command(f"archive {working_dir}/sketch_simple ./my_archives/my_custom_sketch")
507+
result = run_command(f'archive "{working_dir}/sketch_simple" ./my_archives/my_custom_sketch')
507508
assert result.ok
508509

509510
archive = zipfile.ZipFile(f"{working_dir}/my_archives/my_custom_sketch.zip")
@@ -533,7 +534,7 @@ def test_archive_absolute_sketch_path_with_relative_zip_path_and_name_with_exten
533534
archives_folder = f"{working_dir}/my_archives/"
534535
Path(archives_folder).mkdir()
535536

536-
result = run_command(f"archive {working_dir}/sketch_simple ./my_archives/my_custom_sketch.zip")
537+
result = run_command(f'archive "{working_dir}/sketch_simple" ./my_archives/my_custom_sketch.zip')
537538
assert result.ok
538539

539540
archive = zipfile.ZipFile(f"{working_dir}/my_archives/my_custom_sketch.zip")
@@ -563,7 +564,7 @@ def test_archive_absolute_sketch_path_with_absolute_zip_path_and_name_without_ex
563564
archives_folder = f"{working_dir}/my_archives/"
564565
Path(archives_folder).mkdir()
565566

566-
result = run_command(f"archive {working_dir}/sketch_simple {archives_folder}/my_custom_sketch", copy_sketch)
567+
result = run_command(f'archive "{working_dir}/sketch_simple" "{archives_folder}/my_custom_sketch"', copy_sketch)
567568
assert result.ok
568569

569570
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -593,7 +594,7 @@ def test_archive_absolute_sketch_path_with_absolute_zip_path_and_name_with_exten
593594
archives_folder = f"{working_dir}/my_archives/"
594595
Path(archives_folder).mkdir()
595596

596-
result = run_command(f"archive {working_dir}/sketch_simple {archives_folder}/my_custom_sketch.zip", copy_sketch)
597+
result = run_command(f'archive "{working_dir}/sketch_simple" "{archives_folder}/my_custom_sketch.zip"', copy_sketch)
597598
assert result.ok
598599

599600
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -697,7 +698,7 @@ def test_archive_dot_arg_absolute_zip_path_with_include_build_dir_flag(run_comma
697698
archives_folder = f"{working_dir}/my_archives/"
698699
Path(archives_folder).mkdir()
699700

700-
result = run_command(f"archive . {archives_folder} --include-build-dir", copy_sketch)
701+
result = run_command(f'archive . "{archives_folder}" --include-build-dir', copy_sketch)
701702
assert result.ok
702703

703704
archive = zipfile.ZipFile(f"{archives_folder}/sketch_simple.zip")
@@ -757,7 +758,7 @@ def test_archive_dot_arg_absolute_zip_path_and_name_without_extension_with_inclu
757758
archives_folder = f"{working_dir}/my_archives/"
758759
Path(archives_folder).mkdir()
759760

760-
result = run_command(f"archive . {archives_folder}/my_custom_sketch --include-build-dir", copy_sketch)
761+
result = run_command(f'archive . "{archives_folder}/my_custom_sketch" --include-build-dir', copy_sketch)
761762
assert result.ok
762763

763764
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -787,7 +788,7 @@ def test_archive_dot_arg_custom_zip_path_and_name_with_extension_with_include_bu
787788
archives_folder = f"{working_dir}/my_archives/"
788789
Path(archives_folder).mkdir()
789790

790-
result = run_command(f"archive . {archives_folder}/my_custom_sketch.zip --include-build-dir", copy_sketch)
791+
result = run_command(f'archive . "{archives_folder}/my_custom_sketch.zip" --include-build-dir', copy_sketch)
791792
assert result.ok
792793

793794
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -835,7 +836,7 @@ def test_archive_relative_sketch_path_with_include_build_dir_flag(run_command, c
835836

836837

837838
def test_archive_absolute_sketch_path_with_include_build_dir_flag(run_command, copy_sketch, working_dir):
838-
result = run_command(f"archive {working_dir}/sketch_simple --include-build-dir", copy_sketch)
839+
result = run_command(f'archive "{working_dir}/sketch_simple" --include-build-dir', copy_sketch)
839840
assert result.ok
840841

841842
archive = zipfile.ZipFile(f"{working_dir}/sketch_simple.zip")
@@ -895,7 +896,7 @@ def test_archive_relative_sketch_path_with_absolute_zip_path_with_include_build_
895896
archives_folder = f"{working_dir}/my_archives/"
896897
Path(archives_folder).mkdir()
897898

898-
result = run_command(f"archive ./sketch_simple {archives_folder} --include-build-dir")
899+
result = run_command(f'archive ./sketch_simple "{archives_folder}" --include-build-dir')
899900
assert result.ok
900901

901902
archive = zipfile.ZipFile(f"{archives_folder}/sketch_simple.zip")
@@ -985,7 +986,7 @@ def test_archive_relative_sketch_path_with_absolute_zip_path_and_name_without_ex
985986
archives_folder = f"{working_dir}/my_archives/"
986987
Path(archives_folder).mkdir()
987988

988-
result = run_command(f"archive ./sketch_simple {archives_folder}/my_custom_sketch --include-build-dir")
989+
result = run_command(f'archive ./sketch_simple "{archives_folder}/my_custom_sketch" --include-build-dir')
989990
assert result.ok
990991

991992
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -1015,7 +1016,7 @@ def test_archive_relative_sketch_path_with_absolute_zip_path_and_name_with_exten
10151016
archives_folder = f"{working_dir}/my_archives/"
10161017
Path(archives_folder).mkdir()
10171018

1018-
result = run_command(f"archive ./sketch_simple {archives_folder}/my_custom_sketch.zip --include-build-dir")
1019+
result = run_command(f'archive ./sketch_simple "{archives_folder}/my_custom_sketch.zip" --include-build-dir')
10191020
assert result.ok
10201021

10211022
archive = zipfile.ZipFile(f"{archives_folder}/my_custom_sketch.zip")
@@ -1045,7 +1046,7 @@ def test_archive_absolute_sketch_path_with_relative_zip_path_with_include_build_
10451046
archives_folder = f"{working_dir}/my_archives/"
10461047
Path(archives_folder).mkdir()
10471048

1048-
result = run_command(f"archive {working_dir}/sketch_simple ./my_archives --include-build-dir")
1049+
result = run_command(f'archive "{working_dir}/sketch_simple" ./my_archives --include-build-dir')
10491050
assert result.ok
10501051

10511052
archive = zipfile.ZipFile(f"{working_dir}/my_archives/sketch_simple.zip")
@@ -1075,7 +1076,7 @@ def test_archive_absolute_sketch_path_with_absolute_zip_path_with_include_build_
10751076
archives_folder = f"{working_dir}/my_archives/"
10761077
Path(archives_folder).mkdir()
10771078

1078-
result = run_command(f"archive {working_dir}/sketch_simple {archives_folder} --include-build-dir", copy_sketch)
1079+
result = run_command(f'archive "{working_dir}/sketch_simple" "{archives_folder}" --include-build-dir', copy_sketch)
10791080
assert result.ok
10801081

10811082
archive = zipfile.ZipFile(f"{archives_folder}/sketch_simple.zip")
@@ -1105,7 +1106,7 @@ def test_archive_absolute_sketch_path_with_relative_zip_path_and_name_without_ex
11051106
archives_folder = f"{working_dir}/my_archives/"
11061107
Path(archives_folder).mkdir()
11071108

1108-
result = run_command(f"archive {working_dir}/sketch_simple ./my_archives/my_custom_sketch --include-build-dir")
1109+
result = run_command(f'archive "{working_dir}/sketch_simple" ./my_archives/my_custom_sketch --include-build-dir')
11091110
assert result.ok
11101111

11111112
archive = zipfile.ZipFile(f"{working_dir}/my_archives/my_custom_sketch.zip")
@@ -1135,7 +1136,9 @@ def test_archive_absolute_sketch_path_with_relative_zip_path_and_name_with_exten
11351136
archives_folder = f"{working_dir}/my_archives/"
11361137
Path(archives_folder).mkdir()
11371138

1138-
result = run_command(f"archive {working_dir}/sketch_simple ./my_archives/my_custom_sketch.zip --include-build-dir")
1139+
result = run_command(
1140+
f'archive "{working_dir}/sketch_simple" ./my_archives/my_custom_sketch.zip --include-build-dir'
1141+
)
11391142
assert result.ok
11401143

11411144
archive = zipfile.ZipFile(f"{working_dir}/my_archives/my_custom_sketch.zip")
@@ -1166,7 +1169,7 @@ def test_archive_absolute_sketch_path_with_absolute_zip_path_and_name_without_ex
11661169
Path(archives_folder).mkdir()
11671170

11681171
result = run_command(
1169-
f"archive {working_dir}/sketch_simple {archives_folder}/my_custom_sketch --include-build-dir", copy_sketch
1172+
f'archive "{working_dir}/sketch_simple" "{archives_folder}/my_custom_sketch" --include-build-dir', copy_sketch
11701173
)
11711174
assert result.ok
11721175

@@ -1198,7 +1201,8 @@ def test_archive_absolute_sketch_path_with_absolute_zip_path_and_name_with_exten
11981201
Path(archives_folder).mkdir()
11991202

12001203
result = run_command(
1201-
f"archive {working_dir}/sketch_simple {archives_folder}/my_custom_sketch.zip --include-build-dir", copy_sketch
1204+
f'archive "{working_dir}/sketch_simple" "{archives_folder}/my_custom_sketch.zip" --include-build-dir',
1205+
copy_sketch,
12021206
)
12031207
assert result.ok
12041208

Diff for: test/test_config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# otherwise use the software for commercial activities involving the Arduino
1313
# software without disclosing the source code of your own applications. To purchase
1414
# a commercial license, send an email to [email protected].
15-
import os
15+
from pathlib import Path
1616

1717

1818
def test_init(run_command, data_dir, working_dir):
@@ -22,7 +22,7 @@ def test_init(run_command, data_dir, working_dir):
2222

2323

2424
def test_init_dest(run_command, working_dir):
25-
dest = os.path.join(working_dir, "config", "test")
26-
result = run_command("config init --dest-dir " + dest)
25+
dest = str(Path(working_dir) / "config" / "test")
26+
result = run_command(f'config init --dest-dir "{dest}"')
2727
assert result.ok
2828
assert dest in result.stdout

0 commit comments

Comments
 (0)