search_file and test work
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
# prompt user if multiple are found
|
# prompt user if multiple are found
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import errno
|
||||||
from fnmatch import fnmatch
|
from fnmatch import fnmatch
|
||||||
|
|
||||||
def search_all_match(filename, path):
|
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 root, dirs, files in os.walk(path):
|
||||||
for name in files:
|
for name in files:
|
||||||
if fnmatch(filename, name):
|
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
|
return results
|
||||||
|
|
||||||
def print_num_list(paths):
|
def print_num_list(paths):
|
||||||
@@ -40,6 +43,6 @@ def search_file(filename, path):
|
|||||||
paths = search_all_match(filename, path)
|
paths = search_all_match(filename, path)
|
||||||
if len(paths) == 0 :
|
if len(paths) == 0 :
|
||||||
print(f"No match for \"{filename}\". Link not modified.")
|
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)
|
p = select_path(paths)
|
||||||
return p
|
return p
|
||||||
|
|||||||
@@ -1,22 +1,55 @@
|
|||||||
import unittest
|
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!
|
# TODO: gros TODO icitte. Presque rien de fait!
|
||||||
|
|
||||||
class TestSearchFile(unittest.TestCase):
|
class TestSearchFile(unittest.TestCase):
|
||||||
|
|
||||||
data_dir = "test/data/search_test"
|
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):
|
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):
|
@unittest.mock.patch("search_file.input", create=True)
|
||||||
print(search_file("match1.txt", self.data_dir))
|
def test_select_path(self, mocked_input):
|
||||||
with self.assertRaises(FileNotFoundError) as e:
|
print("\n")
|
||||||
search_file("inexistent.txt", self.data_dir)
|
search_file.input.side_effect=["1", "2", "3", "4"]
|
||||||
self.assertTrue("File does not exist", e.exception)
|
search_result = search_all_match(self.target, self.data_dir)
|
||||||
self.assertEqual(search_file("with space.txt", self.data_dir), "test/data/search_test/subdir2/with space.txt")
|
path1 = self.data_dir+"/match1.txt"
|
||||||
print(search_file("match2.txt", self.data_dir))
|
self.assertEqual(select_path(search_result), path1)
|
||||||
print(search_file("subdir2", self.data_dir))
|
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