diff --git a/swh/web/common/service.py b/swh/web/common/service.py --- a/swh/web/common/service.py +++ b/swh/web/common/service.py @@ -297,14 +297,16 @@ directory information as dict. """ + empty_dir_sha1 = hashutil.hash_to_bytes( + '4b825dc642cb6eb9a060e54bf8d69288fbee4904' + ) sha1_git_bin = _to_sha1_bin(sha1_git) - dir = _first_element(storage.directory_get([sha1_git_bin])) - if not dir: - raise NotFoundExc('Directory with sha1_git %s not found' % sha1_git) - directory_entries = storage.directory_ls(sha1_git_bin) or [] - return map(converters.from_directory_entry, directory_entries) + if directory_entries or sha1_git_bin == empty_dir_sha1: + return map(converters.from_directory_entry, directory_entries) + else: + raise NotFoundExc('Directory with sha1_git %s not found' % sha1_git) def lookup_directory_with_path(directory_sha1_git, path_string): diff --git a/swh/web/tests/common/test_service.py b/swh/web/tests/common/test_service.py --- a/swh/web/tests/common/test_service.py +++ b/swh/web/tests/common/test_service.py @@ -1702,19 +1702,19 @@ mock_query.parse_hash_with_algorithms_or_throws.return_value = ( 'sha1', 'directory-id-bin') - mock_storage.directory_get.return_value = None + mock_storage.directory_ls.return_value = [] # when with self.assertRaises(NotFoundExc) as cm: service.lookup_directory('directory_id') - self.assertIn('Directory with sha1_git directory_id not found', - cm.exception.args[0]) + + self.assertIn('Directory with sha1_git directory_id not found', + cm.exception.args[0]) # then mock_query.parse_hash_with_algorithms_or_throws.assert_called_with( 'directory_id', ['sha1'], 'Only sha1_git is supported.') - mock_storage.directory_get.assert_called_with(['directory-id-bin']) - mock_storage.directory_ls.called = False + mock_storage.directory_ls.assert_called_with('directory-id-bin') @patch('swh.web.common.service.storage') @patch('swh.web.common.service.query') @@ -1724,9 +1724,6 @@ 'sha1', 'directory-sha1-bin') - # something that exists is all that matters here - mock_storage.directory_get.return_value = {'id': b'directory-sha1-bin'} - # given stub_dir_entries = [{ 'sha1': self.SHA1_SAMPLE_BIN, @@ -1767,6 +1764,22 @@ mock_storage.directory_ls.assert_called_with( 'directory-sha1-bin') + @patch('swh.web.common.service.storage') + @istest + def lookup_directory_empty(self, mock_storage): + empty_dir_sha1 = '4b825dc642cb6eb9a060e54bf8d69288fbee4904' + mock_storage.directory_ls.return_value = [] + + # when + actual_directory_ls = list(service.lookup_directory(empty_dir_sha1)) + + # then + self.assertEqual(actual_directory_ls, []) + + mock_storage.directory_ls.assert_called_with( + bytes.fromhex(empty_dir_sha1) + ) + @patch('swh.web.common.service.storage') @istest def lookup_revision_by_nothing_found(self, mock_storage):