Skip to content

Commit eac0bbe

Browse files
committed
Move CSV-generation code into its own function and write unit tests
1 parent 43c1264 commit eac0bbe

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

main.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ def get_file_paths_from_csv_file(path_to_csv_file):
7575
return file_paths
7676

7777

78+
def write_to_csv_file(file_paths, path_to_output_file):
79+
"""Writes the specified file paths--and some of their constituent elements--to the specified output file."""
80+
81+
with open(path_to_output_file, 'w', encoding='utf-8', newline='') as f:
82+
writer = csv.writer(f)
83+
84+
# Populate the header row (i.e. column names).
85+
header_row = ["File", "Folder", "Filename", "Suffix (only)"]
86+
writer.writerow(header_row)
87+
88+
# Populate the data rows with the file path, folder path (no filename), filename, and file suffix.
89+
data_rows = []
90+
for file_path in file_paths:
91+
file_suffix = pathlib.Path(file_path).suffix
92+
(folder_path, filename) = os.path.split(file_path)
93+
data_rows.append([file_path, folder_path, filename, file_suffix])
94+
writer.writerows(data_rows)
95+
96+
7897
def main(path_to_folder, path_to_csv_file, path_to_output_file=None, log_level=logging.NOTSET):
7998
"""Entrypoint to the script."""
8099

@@ -107,20 +126,7 @@ def main(path_to_folder, path_to_csv_file, path_to_output_file=None, log_level=l
107126

108127
# Either write the results to a CSV file or print them to STDOUT.
109128
if type(path_to_output_file) is str:
110-
with open(path_to_output_file, 'w', encoding='utf-8', newline='') as f:
111-
writer = csv.writer(f)
112-
113-
# Populate the header row (i.e. column names).
114-
header_row = ["File", "Folder", "Filename", "Suffix (only)"]
115-
writer.writerow(header_row)
116-
117-
# Populate the data rows with the file path, folder path (no filename), filename, and file suffix.
118-
data_rows = []
119-
for path in sorted_differences:
120-
file_suffix = pathlib.Path(path).suffix
121-
(folder_path, filename) = os.path.split(path)
122-
data_rows.append([path, folder_path, filename, file_suffix])
123-
writer.writerows(data_rows)
129+
write_to_csv_file(sorted_differences, path_to_output_file)
124130
else:
125131
print('\n'.join(sorted_differences))
126132

test/test.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import os
12
import unittest
2-
from main import get_file_paths_from_csv_file, get_file_paths_within_folder
3+
from main import (
4+
get_file_paths_from_csv_file,
5+
get_file_paths_within_folder,
6+
write_to_csv_file
7+
)
38
from os import path
49
import tempfile
10+
import csv
511

612

713
class TestGetFilePathsFromCsvFile(unittest.TestCase):
@@ -65,5 +71,50 @@ def test_child_folder(self):
6571
self.assertEqual(paths, expectation)
6672

6773

74+
class TestWriteToCsvFile(unittest.TestCase):
75+
header_row = ["File", "Folder", "Filename", "Suffix (only)"]
76+
temp_file = None
77+
78+
def setUp(self):
79+
"""Create a temporary file before each test."""
80+
self.temp_file = tempfile.NamedTemporaryFile(suffix='.csv', delete=False)
81+
82+
def tearDown(self):
83+
"""Close and delete the temporary file after each test."""
84+
self.temp_file.close()
85+
os.remove(self.temp_file.name)
86+
87+
def test_empty_list(self):
88+
write_to_csv_file([], self.temp_file.name)
89+
with open(self.temp_file.name, 'r', encoding='utf8', newline='') as csv_file:
90+
reader = csv.reader(csv_file)
91+
rows = list(reader)
92+
self.assertEqual(rows, [self.header_row])
93+
94+
def test_nonempty_list(self):
95+
file_paths = [
96+
r"C:/A/B/C", # forward slashes
97+
r"C:\A\B\C", # no suffix
98+
r"C:\A\B\C.d", # suffix
99+
r"C:\A\B\C.d.e", # multiple suffixes
100+
r"C:\A\B\C d.e", # space in file name
101+
r"C:\A B\C", # space in folder name
102+
]
103+
expected_rows = [
104+
self.header_row,
105+
[r"C:/A/B/C", r"C:/A/B", r"C", r""],
106+
[r"C:\A\B\C", r"C:\A\B", r"C", r""],
107+
[r"C:\A\B\C.d", r"C:\A\B", r"C.d", r".d"],
108+
[r"C:\A\B\C.d.e", r"C:\A\B", r"C.d.e", r".e"],
109+
[r"C:\A\B\C d.e", r"C:\A\B", r"C d.e", r".e"],
110+
[r"C:\A B\C", r"C:\A B", r"C", r""],
111+
]
112+
write_to_csv_file(file_paths, self.temp_file.name)
113+
with open(self.temp_file.name, 'r', encoding='utf8', newline='') as csv_file:
114+
reader = csv.reader(csv_file)
115+
rows = list(reader)
116+
self.assertEqual(rows, expected_rows)
117+
118+
68119
if __name__ == '__main__':
69120
unittest.main()

0 commit comments

Comments
 (0)