From 2da6be7618f4052352479e3fb556774efd1e4eab Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Sat, 9 Jul 2022 21:04:28 +0200 Subject: [PATCH] lsp: Fix crash when import version missing The issue is specific to the language server, since it's trying to use an AST that contains errors. The test would not fail but was added anyway. --- CONTRIBUTING.md | 9 +++++++++ blueprintcompiler/language/imports.py | 7 ++++--- tests/sample_errors/no_import_version.blp | 1 + tests/sample_errors/no_import_version.err | 1 + tests/test_samples.py | 1 + 5 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 CONTRIBUTING.md create mode 100644 tests/sample_errors/no_import_version.blp create mode 100644 tests/sample_errors/no_import_version.err diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..b7690a2 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,9 @@ +First of all, thank you for contributing to Blueprint. + +If you learn something useful, please add it to this file. + +# Run the test suite + +``` +python -m unittest +``` diff --git a/blueprintcompiler/language/imports.py b/blueprintcompiler/language/imports.py index 682f8cd..4fac192 100644 --- a/blueprintcompiler/language/imports.py +++ b/blueprintcompiler/language/imports.py @@ -31,16 +31,17 @@ class GtkDirective(AstNode): @validate("version") def gtk_version(self): - if self.tokens["version"] not in ["4.0"]: + version = self.tokens["version"] + if version not in ["4.0"]: err = CompileError("Only GTK 4 is supported") - if self.tokens["version"].startswith("4"): + if version and version.startswith("4"): err.hint("Expected the GIR version, not an exact version number. Use 'using Gtk 4.0;'.") else: err.hint("Expected 'using Gtk 4.0;'") raise err try: - gir.get_namespace("Gtk", self.tokens["version"]) + gir.get_namespace("Gtk", version) except CompileError as e: raise CompileError( "Could not find GTK 4 introspection files. Is gobject-introspection installed?", diff --git a/tests/sample_errors/no_import_version.blp b/tests/sample_errors/no_import_version.blp new file mode 100644 index 0000000..665ce62 --- /dev/null +++ b/tests/sample_errors/no_import_version.blp @@ -0,0 +1 @@ +using Gtk \ No newline at end of file diff --git a/tests/sample_errors/no_import_version.err b/tests/sample_errors/no_import_version.err new file mode 100644 index 0000000..4ee792f --- /dev/null +++ b/tests/sample_errors/no_import_version.err @@ -0,0 +1 @@ +1,10,0,Expected a version number for GTK diff --git a/tests/test_samples.py b/tests/test_samples.py index 84cdb4f..ea6b2ea 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -188,6 +188,7 @@ class TestSamples(unittest.TestCase): self.assert_sample_error("gtk_exact_version") self.assert_sample_error("invalid_bool") self.assert_sample_error("layout_in_non_widget") + self.assert_sample_error("no_import_version") self.assert_sample_error("ns_not_imported") self.assert_sample_error("not_a_class") self.assert_sample_error("object_dne")