|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pre_commit_hooks.check_fluent import main |
| 6 | + |
| 7 | + |
| 8 | +def test_valid_fluent_file(tmp_path): |
| 9 | + f = tmp_path / 'test.ftl' |
| 10 | + f.write_text( |
| 11 | + 'hello = Hello, world!\n' |
| 12 | + 'greeting = Hello, { $name }!\n' |
| 13 | + ' .title = Greeting\n' |
| 14 | + 'menu-item = Menu Item\n', |
| 15 | + ) |
| 16 | + assert main([str(f)]) == 0 |
| 17 | + |
| 18 | + |
| 19 | +def test_fluent_file_with_select_expression(tmp_path): |
| 20 | + f = tmp_path / 'test.ftl' |
| 21 | + f.write_text( |
| 22 | + 'emails = { $unreadEmails ->\n' |
| 23 | + ' [0] You have no unread emails.\n' |
| 24 | + ' [one] You have one unread email.\n' |
| 25 | + ' *[other] You have { $unreadEmails } unread emails.\n' |
| 26 | + '}\n', |
| 27 | + ) |
| 28 | + assert main([str(f)]) == 0 |
| 29 | + |
| 30 | + |
| 31 | +def test_fluent_file_with_comments(tmp_path): |
| 32 | + f = tmp_path / 'test.ftl' |
| 33 | + f.write_text( |
| 34 | + '# This is a comment\n' |
| 35 | + 'hello = Hello, world!\n' |
| 36 | + '\n' |
| 37 | + '## Another comment\n' |
| 38 | + 'goodbye = Goodbye!\n', |
| 39 | + ) |
| 40 | + assert main([str(f)]) == 0 |
| 41 | + |
| 42 | + |
| 43 | +def test_fluent_file_with_invalid_identifier(tmp_path): |
| 44 | + f = tmp_path / 'test.ftl' |
| 45 | + f.write_text('123invalid = Invalid identifier\n') |
| 46 | + assert main([str(f)]) == 1 |
| 47 | + |
| 48 | + |
| 49 | +def test_fluent_file_with_invalid_attribute_identifier(tmp_path): |
| 50 | + f = tmp_path / 'test.ftl' |
| 51 | + f.write_text('hello = Hello\n' ' .123invalid = Invalid attribute\n') |
| 52 | + assert main([str(f)]) == 1 |
| 53 | + |
| 54 | + |
| 55 | +def test_fluent_file_missing_default_variant(tmp_path): |
| 56 | + f = tmp_path / 'test.ftl' |
| 57 | + f.write_text( |
| 58 | + 'emails = { $unreadEmails ->\n' |
| 59 | + ' [0] You have no unread emails.\n' |
| 60 | + ' [one] You have one unread email.\n' |
| 61 | + '}\n', |
| 62 | + ) |
| 63 | + assert main([str(f)]) == 1 |
| 64 | + |
| 65 | + |
| 66 | +def test_fluent_file_variant_outside_select(tmp_path): |
| 67 | + f = tmp_path / 'test.ftl' |
| 68 | + f.write_text('hello = Hello\n' ' *[default] This should not be here\n') |
| 69 | + assert main([str(f)]) == 1 |
| 70 | + |
| 71 | + |
| 72 | +def test_fluent_file_missing_indentation(tmp_path): |
| 73 | + f = tmp_path / 'test.ftl' |
| 74 | + f.write_text('hello = Hello\n' '.title = This should be indented\n') |
| 75 | + assert main([str(f)]) == 1 |
| 76 | + |
| 77 | + |
| 78 | +def test_fluent_file_indented_without_context(tmp_path): |
| 79 | + f = tmp_path / 'test.ftl' |
| 80 | + f.write_text(' orphaned = This line has no message context\n') |
| 81 | + assert main([str(f)]) == 1 |
| 82 | + |
| 83 | + |
| 84 | +def test_non_utf8_file(tmp_path): |
| 85 | + f = tmp_path / 'test.ftl' |
| 86 | + f.write_bytes(b'\xa9\xfe\x12') |
| 87 | + assert main([str(f)]) == 1 |
| 88 | + |
| 89 | + |
| 90 | +def test_nonexistent_file(): |
| 91 | + assert main(['nonexistent.ftl']) == 1 |
| 92 | + |
| 93 | + |
| 94 | +def test_empty_file(tmp_path): |
| 95 | + f = tmp_path / 'test.ftl' |
| 96 | + f.write_text('') |
| 97 | + assert main([str(f)]) == 0 |
| 98 | + |
| 99 | + |
| 100 | +def test_multiple_files(tmp_path): |
| 101 | + f1 = tmp_path / 'valid.ftl' |
| 102 | + f1.write_text('hello = Hello, world!\n') |
| 103 | + |
| 104 | + f2 = tmp_path / 'invalid.ftl' |
| 105 | + f2.write_text('123invalid = Invalid identifier\n') |
| 106 | + |
| 107 | + assert main([str(f1), str(f2)]) == 1 |
| 108 | + |
| 109 | + |
| 110 | +def test_multiple_valid_files(tmp_path): |
| 111 | + f1 = tmp_path / 'valid1.ftl' |
| 112 | + f1.write_text('hello = Hello, world!\n') |
| 113 | + |
| 114 | + f2 = tmp_path / 'valid2.ftl' |
| 115 | + f2.write_text('goodbye = Goodbye!\n') |
| 116 | + |
| 117 | + assert main([str(f1), str(f2)]) == 0 |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize( |
| 121 | + 'identifier,expected', |
| 122 | + [ |
| 123 | + ('hello', True), |
| 124 | + ('hello-world', True), |
| 125 | + ('hello_world', True), |
| 126 | + ('hello123', True), |
| 127 | + ('123hello', False), |
| 128 | + ('hello-', True), |
| 129 | + ('-hello', False), |
| 130 | + ('', False), |
| 131 | + ('hello.world', False), |
| 132 | + ('hello world', False), |
| 133 | + ], |
| 134 | +) |
| 135 | +def test_identifier_validation(identifier, expected): |
| 136 | + from pre_commit_hooks.check_fluent import _is_valid_identifier |
| 137 | + |
| 138 | + assert _is_valid_identifier(identifier) == expected |
| 139 | + |
| 140 | + |
| 141 | +def test_fluent_file_non_default_variant_with_closing_brace(tmp_path): |
| 142 | + f = tmp_path / 'test.ftl' |
| 143 | + f.write_text( |
| 144 | + 'emails = { $unreadEmails ->\n' |
| 145 | + ' [0] You have no unread emails. }\n', |
| 146 | + ) |
| 147 | + assert main([str(f)]) == 1 # Should fail due to missing default variant |
| 148 | + |
| 149 | + |
| 150 | +def test_fluent_file_non_star_variant_with_closing_check(tmp_path): |
| 151 | + f = tmp_path / 'test.ftl' |
| 152 | + f.write_text( |
| 153 | + 'test = { $var ->\n' |
| 154 | + ' [case]\n' # Comment |
| 155 | + ' Value here\n' |
| 156 | + ' *[other] Default\n' |
| 157 | + '}\n', |
| 158 | + ) |
| 159 | + assert main([str(f)]) == 0 |
0 commit comments