|
1 | 1 | import typing as t |
| 2 | +import os |
2 | 3 | from lsprotocol import types |
3 | 4 | from sqlmesh.core.context import Context |
4 | 5 | from sqlmesh.lsp.context import LSPContext |
@@ -109,3 +110,73 @@ def test_code_actions_with_linting(copy_to_temp_path: t.Callable): |
109 | 110 | URI.from_path(sushi_path / "models" / "latest_order.sql").value |
110 | 111 | ] |
111 | 112 | assert len(text_edits) > 0 |
| 113 | + |
| 114 | + |
| 115 | +def test_code_actions_create_file(copy_to_temp_path: t.Callable) -> None: |
| 116 | + sushi_paths = copy_to_temp_path("examples/sushi") |
| 117 | + sushi_path = sushi_paths[0] |
| 118 | + |
| 119 | + # Remove external models file and enable linter |
| 120 | + os.remove(sushi_path / "external_models.yaml") |
| 121 | + config_path = sushi_path / "config.py" |
| 122 | + with config_path.open("r") as f: |
| 123 | + content = f.read() |
| 124 | + |
| 125 | + before = """ linter=LinterConfig( |
| 126 | + enabled=False, |
| 127 | + rules=[ |
| 128 | + "ambiguousorinvalidcolumn", |
| 129 | + "invalidselectstarexpansion", |
| 130 | + "noselectstar", |
| 131 | + "nomissingaudits", |
| 132 | + "nomissingowner", |
| 133 | + "nomissingexternalmodels", |
| 134 | + ], |
| 135 | + ),""" |
| 136 | + after = """linter=LinterConfig(enabled=True, rules=["nomissingexternalmodels"]),""" |
| 137 | + content = content.replace(before, after) |
| 138 | + with config_path.open("w") as f: |
| 139 | + f.write(content) |
| 140 | + |
| 141 | + context = Context(paths=[str(sushi_path)]) |
| 142 | + lsp_context = LSPContext(context) |
| 143 | + |
| 144 | + uri = URI.from_path(sushi_path / "models" / "customers.sql") |
| 145 | + violations = lsp_context.lint_model(uri) |
| 146 | + |
| 147 | + diagnostics = [] |
| 148 | + for violation in violations: |
| 149 | + if violation.violation_range: |
| 150 | + diagnostics.append( |
| 151 | + types.Diagnostic( |
| 152 | + range=types.Range( |
| 153 | + start=types.Position( |
| 154 | + line=violation.violation_range.start.line, |
| 155 | + character=violation.violation_range.start.character, |
| 156 | + ), |
| 157 | + end=types.Position( |
| 158 | + line=violation.violation_range.end.line, |
| 159 | + character=violation.violation_range.end.character, |
| 160 | + ), |
| 161 | + ), |
| 162 | + message=violation.violation_msg, |
| 163 | + severity=types.DiagnosticSeverity.Warning, |
| 164 | + ) |
| 165 | + ) |
| 166 | + |
| 167 | + params = types.CodeActionParams( |
| 168 | + text_document=types.TextDocumentIdentifier(uri=uri.value), |
| 169 | + range=types.Range( |
| 170 | + start=types.Position(line=0, character=0), end=types.Position(line=1, character=0) |
| 171 | + ), |
| 172 | + context=types.CodeActionContext(diagnostics=diagnostics), |
| 173 | + ) |
| 174 | + |
| 175 | + actions = lsp_context.get_code_actions(uri, params) |
| 176 | + assert actions is not None and len(actions) > 0 |
| 177 | + action = next(a for a in actions if isinstance(a, types.CodeAction)) |
| 178 | + assert action.edit is not None |
| 179 | + assert action.edit.document_changes is not None |
| 180 | + create_file = [c for c in action.edit.document_changes if isinstance(c, types.CreateFile)] |
| 181 | + assert create_file, "Expected a CreateFile operation" |
| 182 | + assert create_file[0].uri == URI.from_path(sushi_path / "external_models.yaml").value |
0 commit comments