Skip to content

Commit e0e5a59

Browse files
committed
Further refactor Server sketch creator, improve tests, 100% module coverage
1 parent 77109de commit e0e5a59

File tree

2 files changed

+118
-103
lines changed

2 files changed

+118
-103
lines changed

ardublocklyserver/sketchcreator.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# SketchCreator class creates an Arduino Sketch source code file.
3+
# Sketch Creator module creates an Arduino Sketch source code file.
44
#
55
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
66
# Licensed under the Apache License, Version 2.0 (the "License"):
@@ -29,7 +29,8 @@
2929
default_sketch_name = 'ArdublocklySketch'
3030

3131

32-
def create_sketch(sketch_dir, sketch_name=None, sketch_code=None):
32+
def create_sketch(sketch_dir, sketch_name=default_sketch_name,
33+
sketch_code=default_sketch_code):
3334
"""Create an Arduino Sketch file into the given directory.
3435
3536
Creates an Arduino sketch with either the default blinky code or the
@@ -42,26 +43,17 @@ def create_sketch(sketch_dir, sketch_name=None, sketch_code=None):
4243
Return None indicates an error has occurred.
4344
"""
4445
# Check the code first, to not create sketch file if invalid
45-
if sketch_code is None:
46-
code_to_write = default_sketch_code
47-
else:
48-
if isinstance(sketch_code, six.string_types):
49-
code_to_write = sketch_code
50-
else:
51-
print('The sketch code given is not a valid string !!!')
52-
return None
53-
# Check validity and create the sketch path
54-
if sketch_name is None:
55-
sketch_name = default_sketch_name
46+
if not isinstance(sketch_code, six.string_types):
47+
print('The sketch code given is not a valid string !!!')
48+
return None
49+
# Create the sketch path
5650
sketch_path = build_sketch_path(sketch_dir, sketch_name)
5751
try:
5852
with codecs.open(sketch_path, 'wb+', encoding='utf-8') as sketch_f:
59-
sketch_f.write(code_to_write)
53+
sketch_f.write(sketch_code)
6054
except Exception as e:
61-
print(e)
62-
print('Arduino sketch could not be created !!!')
55+
print('Error: %s\nArduino sketch could not be created !!!' % e)
6356
return None
64-
6557
return sketch_path
6658

6759

@@ -76,10 +68,14 @@ def build_sketch_path(sketch_dir, sketch_name):
7668
"""
7769
sketch_path = None
7870
if os.path.isdir(sketch_dir):
79-
sketch_path = os.path.join(sketch_dir, sketch_name)
80-
if not os.path.exists(sketch_path):
81-
os.makedirs(sketch_path)
82-
sketch_path = os.path.join(sketch_path, sketch_name + '.ino')
71+
try:
72+
sketch_path = os.path.join(sketch_dir, sketch_name)
73+
except TypeError as e:
74+
print('Error: %s\nSketch Name could not be processed.' % e)
75+
else:
76+
if not os.path.exists(sketch_path):
77+
os.makedirs(sketch_path)
78+
sketch_path = os.path.join(sketch_path, sketch_name + '.ino')
8379
else:
8480
print('The sketch directory "%s" does not exists !!!' % sketch_dir)
8581
return sketch_path

ardublocklyserver/tests/sketchcreator_test.py

Lines changed: 101 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Unit test for the sketchcreator module.
3+
# Unit test for the Sketch Creator module module.
44
#
5-
# Copyright (c) 2015 carlosperate https://github.com/carlosperate/
5+
# Copyright (c) 2017 carlosperate https://github.com/carlosperate/
66
# Licensed under the Apache License, Version 2.0 (the "License"):
77
# http://www.apache.org/licenses/LICENSE-2.0
88
#
@@ -27,101 +27,120 @@
2727

2828
class SketchCreatorTestCase(unittest.TestCase):
2929
"""
30-
Tests for SketchCreator class
30+
Tests for SketchCreator class.
31+
Rather than mocking around with os module it creates 'safe' folder inside
32+
the test directory where it can create and delete files.
3133
"""
3234

3335
#
34-
# File creation without input code
36+
# Test fixtures
3537
#
36-
def test_create_sketch(self):
37-
""" Tests to see if an Arduino Sketch is created in a new location. """
38-
# First test with the default name
39-
sketch_dir = os.getcwd()
40-
final_ino_path = os.path.join(
41-
sketch_dir,
42-
sketchcreator.default_sketch_name,
38+
@classmethod
39+
def setUpClass(cls):
40+
"""Create a temporary folder to play round."""
41+
cls.temp_folder = os.path.join(
42+
os.path.abspath(os.path.dirname(__file__)),
43+
'TestTemp_sketchcreator')
44+
if os.path.isdir(cls.temp_folder):
45+
raise Exception('Directory %s already exists.' % cls.temp_folder)
46+
os.makedirs(cls.temp_folder)
47+
# Put together the default sketch path
48+
cls.default_sketch_path = os.path.join(
49+
cls.temp_folder, sketchcreator.default_sketch_name,
4350
'%s.ino' % sketchcreator.default_sketch_name)
44-
# Should be safe to create and delete the ino file in the test folder
45-
if os.path.exists(final_ino_path):
46-
os.remove(final_ino_path)
47-
self.assertFalse(os.path.isfile(final_ino_path))
48-
# Checks the file is saved, and saved to the right location
49-
created_sketch_path = sketchcreator.create_sketch(sketch_dir)
50-
self.assertEqual(final_ino_path, created_sketch_path)
51-
self.assertTrue(os.path.isfile(final_ino_path))
5251

53-
# Now test with a given sketch name and a unicode path
54-
sketch_name = 'TestTemp_Sketch'
55-
sketch_dir_unicode = os.path.join(os.getcwd(), 'TestTemp_ろΓαζςÂé')
56-
final_ino_path = os.path.join(sketch_dir_unicode,
57-
sketch_name,
58-
sketch_name + '.ino')
59-
60-
# Test directory should be a safe place to create this unicode folder
61-
if not os.path.exists(sketch_dir_unicode):
62-
os.mkdir(sketch_dir_unicode)
63-
else:
64-
# We count on this specific folder name to be too unlikely
65-
# created by a user and not this unit test, so remove contents
66-
shutil.rmtree(sketch_dir_unicode)
67-
os.mkdir(sketch_dir_unicode)
68-
69-
self.assertFalse(os.path.isfile(final_ino_path))
70-
71-
# Checks the file is saved, and saved to the right location
52+
@classmethod
53+
def tearDownClass(cls):
54+
"""Deletes the previously created temporary folder."""
55+
if os.path.isdir(cls.temp_folder):
56+
shutil.rmtree(cls.temp_folder)
57+
58+
def setUp(self):
59+
"""Ensure the temp folder exists."""
60+
if not os.path.isdir(self.__class__.temp_folder):
61+
os.makedirs(self.__class__.temp_folder)
62+
63+
def tearDown(self):
64+
"""Delete any files created inside the temp directory"""
65+
if os.path.isdir(self.__class__.temp_folder):
66+
shutil.rmtree(self.__class__.temp_folder)
67+
68+
#
69+
# Tests for file creation
70+
#
71+
def test_sketch_name_default(self):
72+
"""Test default sketch has created the file correctly."""
73+
sketch_path = sketchcreator.create_sketch(self.temp_folder)
74+
75+
self.assertEqual(sketch_path, self.default_sketch_path)
76+
self.assertTrue(os.path.isfile(self.default_sketch_path))
77+
78+
def test_sketch_name_non_default(self):
79+
"""Tests to see if an Arduino Sketch is created in a new location."""
80+
filename_unicode = 'TestTemp_ろΓαζςÂé'
81+
final_ino_path = os.path.join(
82+
self.temp_folder, filename_unicode, filename_unicode + '.ino')
83+
7284
created_sketch_path = sketchcreator.create_sketch(
73-
sketch_dir_unicode, sketch_name=sketch_name)
85+
self.temp_folder, sketch_name=filename_unicode)
86+
7487
self.assertEqual(final_ino_path, created_sketch_path)
7588
self.assertTrue(os.path.isfile(final_ino_path))
7689

77-
# Remove created unicode dir
78-
shutil.rmtree(sketch_dir_unicode)
79-
80-
def test_create_sketch_invalid(self):
81-
"""
82-
Test for invalid inputs in the create_sketch method.
83-
"""
84-
# Test for failure on invalid sketch path
85-
random_invalid_path = os.path.join(os.getcwd(), 'raNd_dIr')
86-
self.assertFalse(os.path.isdir(random_invalid_path))
87-
created_sketch_path = sketchcreator.create_sketch(random_invalid_path)
88-
self.assertIsNone(created_sketch_path)
89-
self.assertFalse(os.path.isdir(random_invalid_path))
90-
91-
# Test for failure on invalid sketch code
92-
sketch_path = os.getcwd()
93-
sketch_folder_path = os.path.join(
94-
sketch_path, sketchcreator.default_sketch_name)
95-
if os.path.isdir(sketch_folder_path):
96-
shutil.rmtree(sketch_folder_path)
97-
self.assertFalse(os.path.isdir(sketch_folder_path))
98-
invalid_sketch_code = True
90+
def test_sketch_name_invalid(self):
91+
"""Test for invalid inputs in the create_sketch method."""
92+
self.assertFalse(os.path.isdir(self.default_sketch_path))
93+
invalid_sketch_name = True
94+
9995
created_sketch_path = sketchcreator.create_sketch(
100-
sketch_path, sketch_code=invalid_sketch_code)
96+
self.temp_folder, sketch_name=invalid_sketch_name)
97+
98+
self.assertIsNone(created_sketch_path)
99+
self.assertFalse(os.path.isdir(self.default_sketch_path))
100+
101+
def test_sketch_path_invalid(self):
102+
"""Test for invalid inputs in the create_sketch method."""
103+
invalid_path = os.path.join(self.temp_folder, 'raNd_dIr')
104+
self.assertFalse(os.path.isdir(invalid_path))
105+
106+
created_sketch_path = sketchcreator.create_sketch(invalid_path)
107+
101108
self.assertIsNone(created_sketch_path)
102-
self.assertFalse(os.path.isdir(sketch_folder_path))
109+
self.assertFalse(os.path.isdir(invalid_path))
103110

104111
#
105-
# File creation with code
112+
# Tests for code content
106113
#
107-
def test_create_sketch_with_code(self):
108-
sketch_dir = os.getcwd()
109-
sketch_ino_location = os.path.join(
110-
sketch_dir,
111-
sketchcreator.default_sketch_name,
112-
'%s.ino' % sketchcreator.default_sketch_name)
113-
sketch_code_write = 'Unicode test (ろΓαζςÂaé) on: %s' % \
114-
time.strftime("%Y-%m-%d %H:%M:%S")
115-
116-
sketch_return_location = sketchcreator.create_sketch(
117-
sketch_dir, sketch_code=sketch_code_write)
118-
self.assertEqual(sketch_return_location, sketch_ino_location)
119-
120-
arduino_sketch = codecs.open(
121-
sketch_return_location, 'r', encoding='utf-8')
122-
sketch_code_read = arduino_sketch.read()
123-
arduino_sketch.close()
124-
self.assertEqual(sketch_code_write, sketch_code_read)
114+
def test_sketch_code_default(self):
115+
"""Test default sketch has filled the sketch contents correctly."""
116+
sketch_path = sketchcreator.create_sketch(self.temp_folder)
117+
118+
with codecs.open(sketch_path, 'r', encoding='utf-8') as sketch:
119+
sketch_content = sketch.read()
120+
self.assertEqual(sketch_content, sketchcreator.default_sketch_code)
121+
122+
def test_sketch_code_non_default(self):
123+
"""Test sketch is created correctly with the given code."""
124+
sketch_code = 'Unicode test (ろΓαζςÂaé) on: %s' % \
125+
time.strftime("%Y-%m-%d %H:%M:%S")
126+
127+
sketch_path = sketchcreator.create_sketch(
128+
self.temp_folder, sketch_code=sketch_code)
129+
130+
with codecs.open(sketch_path, 'r', encoding='utf-8') as sketch:
131+
sketch_code_read = sketch.read()
132+
self.assertEqual(sketch_code_read, sketch_code)
133+
134+
def test_sketch_code_invalid(self):
135+
"""Test for invalid inputs in the create_sketch method."""
136+
self.assertFalse(os.path.isdir(self.default_sketch_path))
137+
invalid_sketch_code = True
138+
139+
created_sketch_path = sketchcreator.create_sketch(
140+
self.temp_folder, sketch_code=invalid_sketch_code)
141+
142+
self.assertIsNone(created_sketch_path)
143+
self.assertFalse(os.path.isdir(self.default_sketch_path))
125144

126145

127146
if __name__ == '__main__':

0 commit comments

Comments
 (0)