search_file and test work
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
# prompt user if multiple are found
|
||||
|
||||
import os
|
||||
import errno
|
||||
from fnmatch import fnmatch
|
||||
|
||||
def search_all_match(filename, path):
|
||||
@@ -12,7 +13,9 @@ def search_all_match(filename, path):
|
||||
for root, dirs, files in os.walk(path):
|
||||
for name in files:
|
||||
if fnmatch(filename, name):
|
||||
results.append(os.path.join(root, name))
|
||||
match_path = os.path.join(root, name)
|
||||
if not os.path.islink(match_path):
|
||||
results.append(match_path)
|
||||
return results
|
||||
|
||||
def print_num_list(paths):
|
||||
@@ -40,6 +43,6 @@ def search_file(filename, path):
|
||||
paths = search_all_match(filename, path)
|
||||
if len(paths) == 0 :
|
||||
print(f"No match for \"{filename}\". Link not modified.")
|
||||
raise Exception(FileNotFoundError)
|
||||
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
|
||||
p = select_path(paths)
|
||||
return p
|
||||
|
||||
@@ -1,22 +1,55 @@
|
||||
import unittest
|
||||
from search_file import search_file
|
||||
import unittest.mock
|
||||
import io
|
||||
from search_file import print_num_list, search_all_match, select_path
|
||||
import search_file
|
||||
|
||||
# TODO: gros TODO icitte. Presque rien de fait!
|
||||
|
||||
class TestSearchFile(unittest.TestCase):
|
||||
|
||||
data_dir = "test/data/search_test"
|
||||
mock_list = [data_dir+"/match1.txt",\
|
||||
data_dir+"/subdir21/match1.txt",\
|
||||
data_dir+"/subdir1/match1.txt",\
|
||||
data_dir+"/subdir2/match1.txt"]
|
||||
|
||||
print_mock = "1. test/data/search_test/match1.txt\n\
|
||||
2. test/data/search_test/subdir21/match1.txt\n\
|
||||
3. test/data/search_test/subdir1/match1.txt\n\
|
||||
4. test/data/search_test/subdir2/match1.txt\n"
|
||||
|
||||
target = "match1.txt"
|
||||
|
||||
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||
def assert_stdout(self, lst, expected_output, mock_stdout):
|
||||
print_num_list(lst)
|
||||
self.assertEqual(mock_stdout.getvalue(), expected_output)
|
||||
|
||||
def test_print_num_list(self):
|
||||
p = [""]
|
||||
self.assert_stdout(self.mock_list, self.print_mock)
|
||||
|
||||
def test_search_all_match(self):
|
||||
search_result = search_all_match(self.target, self.data_dir)
|
||||
self.assertEqual(search_result, self.mock_list)
|
||||
search_result = search_all_match("inexistent.txt", self.data_dir)
|
||||
self.assertEqual(search_result, [])
|
||||
|
||||
def test_search_file(self):
|
||||
print(search_file("match1.txt", self.data_dir))
|
||||
with self.assertRaises(FileNotFoundError) as e:
|
||||
search_file("inexistent.txt", self.data_dir)
|
||||
self.assertTrue("File does not exist", e.exception)
|
||||
self.assertEqual(search_file("with space.txt", self.data_dir), "test/data/search_test/subdir2/with space.txt")
|
||||
print(search_file("match2.txt", self.data_dir))
|
||||
print(search_file("subdir2", self.data_dir))
|
||||
@unittest.mock.patch("search_file.input", create=True)
|
||||
def test_select_path(self, mocked_input):
|
||||
print("\n")
|
||||
search_file.input.side_effect=["1", "2", "3", "4"]
|
||||
search_result = search_all_match(self.target, self.data_dir)
|
||||
path1 = self.data_dir+"/match1.txt"
|
||||
self.assertEqual(select_path(search_result), path1)
|
||||
path2 = self.data_dir+"/subdir21/match1.txt"
|
||||
self.assertEqual(select_path(search_result), path2)
|
||||
|
||||
@unittest.mock.patch("search_file.input", create=True)
|
||||
def test_search_file(self, mock_input):
|
||||
search_file.input.side_effect=["1"]
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
search_file.search_file("inexistent.txt", self.data_dir)
|
||||
search_result = search_all_match(self.target, self.data_dir)
|
||||
self.assertTrue(search_result, self.data_dir+"/match1.txt")
|
||||
self.assertEqual(search_file.search_file("with space.txt", self.data_dir), "test/data/search_test/subdir2/with space.txt")
|
||||
|
||||
Reference in New Issue
Block a user