Skip to content

Commit

Permalink
🐛 Fix CLI when using --minimal output in certain cases (#103)
Browse files Browse the repository at this point in the history
* 🐛 Fix CLI when using --minimal output in certain cases

Happen when used with -a (alternative)

* ✔️ Add tests
  • Loading branch information
Ousret authored Sep 16, 2021
1 parent 4db2367 commit 26be638
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
13 changes: 11 additions & 2 deletions charset_normalizer/cli/normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def cli_detect(argv: List[str] = None) -> int:

if len(matches) > 1 and args.alternatives:
for el in matches:
if el != matches:
if el != best_guess:
x_.append(
CliDetectionResult(
abspath(my_file.name),
Expand Down Expand Up @@ -273,7 +273,16 @@ def cli_detect(argv: List[str] = None) -> int:
)
)
else:
print(", ".join([el.encoding if el.encoding else "undefined" for el in x_]))
for my_file in args.files:
print(
", ".join(
[
el.encoding if el.encoding else "undefined"
for el in x_
if el.path == abspath(my_file.name)
]
)
)

return 0

Expand Down
40 changes: 40 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,46 @@ def test_multiple_file(self):
)
)

def test_with_alternative(self):
self.assertEqual(
0,
cli_detect(
[
'-a',
'./data/sample.1.ar.srt',
'./data/sample.1.he.srt',
'./data/sample-chinese.txt'
]
)
)

def test_with_minimal_output(self):
self.assertEqual(
0,
cli_detect(
[
'-m',
'./data/sample.1.ar.srt',
'./data/sample.1.he.srt',
'./data/sample-chinese.txt'
]
)
)

def test_with_minimal_and_alt(self):
self.assertEqual(
0,
cli_detect(
[
'-m',
'-a',
'./data/sample.1.ar.srt',
'./data/sample.1.he.srt',
'./data/sample-chinese.txt'
]
)
)

def test_non_existent_file(self):

with self.assertRaises(SystemExit) as cm:
Expand Down

0 comments on commit 26be638

Please sign in to comment.