Skip to content

Commit

Permalink
Fix Python error name regression
Browse files Browse the repository at this point in the history
  • Loading branch information
bendk committed Sep 19, 2024
1 parent 8aec9df commit c682669
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

- Added the `GenerationSettings::mode` field. This can be ignored in most cases, it's currently only used by Swift.

### What's fixed?
- Python: Fixed a bug when enum/error names were not proper camel case (HTMLError instead of HtmlError).

[All changes in [[UnreleasedUniFFIVersion]]](https://github.com/mozilla/uniffi-rs/compare/v0.28.1...HEAD).

## v0.28.1 (backend crates: v0.28.1) - (_2024-08-09_)
Expand Down
10 changes: 10 additions & 0 deletions fixtures/coverall/src/coverall.udl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace coverall {
Getters test_round_trip_through_rust(Getters getters);
void test_round_trip_through_foreign(Getters getters);

// Always throws an InvalidHTML error
[Throws=HTMLError]
void validate_html(string source);
};

dictionary SimpleDict {
Expand Down Expand Up @@ -295,3 +298,10 @@ interface ISecond {
};

dictionary EmptyStruct {};

// Error class with a controversial camel case. This can cause errors if we don't consistently
// convert the name.
[Error]
enum HTMLError {
"InvalidHTML"
};
10 changes: 10 additions & 0 deletions fixtures/coverall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,14 @@ impl ISecond {

pub struct EmptyStruct;

#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
pub enum HTMLError {
#[error("InvalidHTML")]
InvalidHTML,
}

pub fn validate_html(_source: String) -> Result<(), HTMLError> {
Err(HTMLError::InvalidHTML)
}

uniffi::include_scaffolding!("coverall");
11 changes: 11 additions & 0 deletions fixtures/coverall/tests/bindings/test_coverall.kts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,17 @@ Coveralls("using_fakes_with_real_objects_crashes").use { coveralls ->
assert(exception != null)
}

Coveralls("HtmlError").use { coveralls ->
var exception: Throwable? = null
try {
validateHtml("test")
} catch (e: HtmlException.InvalidHtml) {
exception = e
}
assert(exception != null)
}


// This is from an earlier GC test; ealier, we made 1000 new objects.
// By now, the GC has had time to clean up, and now we should see 0 alive.
// (hah! Wishful-thinking there ;)
Expand Down
4 changes: 4 additions & 0 deletions fixtures/coverall/tests/bindings/test_coverall.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,5 +454,9 @@ def test_rust_only_traits(self):
self.assertEqual(traits[0].concat("cow", "boy"), "cowboy")
self.assertEqual(traits[1].concat("cow", "boy"), "cowboy")

def test_html_error(self):
with self.assertRaises(HtmlError):
validate_html("test")

if __name__=='__main__':
unittest.main()
6 changes: 6 additions & 0 deletions fixtures/coverall/tests/bindings/test_coverall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,10 @@ def test_bytes
assert_equal coveralls.reverse("123").encoding, Encoding::BINARY
end

def test_html_error
assert_raise Coverall::HtmlError::InvalidHtml do
Coverall.validate_html("test")
end
end

end
10 changes: 10 additions & 0 deletions fixtures/coverall/tests/bindings/test_coverall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,13 @@ do {
assert(stringUtils[0].concat(a: "cow", b: "boy") == "cowboy")
assert(stringUtils[1].concat(a: "cow", b: "boy") == "cowboy")
}

// Test HTMLError
do {
try validateHtml(source: "test")
fatalError("should have thrown")
} catch HtmlError.InvalidHtml {
// Expected
} catch {
fatalError("Unexpected error: \(error)")
}
2 changes: 1 addition & 1 deletion uniffi_bindgen/src/bindings/python/gen_python/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl CodeType for EnumCodeType {
}

fn canonical_name(&self) -> String {
format!("Type{}", self.id)
format!("Type{}", self.type_label())
}

fn literal(&self, literal: &Literal) -> String {
Expand Down
2 changes: 1 addition & 1 deletion uniffi_bindgen/src/bindings/python/gen_python/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl CodeType for ErrorCodeType {
}

fn canonical_name(&self) -> String {
format!("Type{}", self.id)
format!("Type{}", self.type_label())
}

fn literal(&self, _literal: &Literal) -> String {
Expand Down
5 changes: 5 additions & 0 deletions uniffi_bindgen/src/bindings/python/gen_python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,11 @@ impl VisitMut for PythonCodeOracle {
//TODO: Renaming the function name in wrapper.py is not currently tested
function.rename(self.fn_name(function.name()));
}

fn visit_error_name(&self, name: &mut String) {
*name = self.class_name(name);
}

}

trait AsCodeType {
Expand Down
8 changes: 8 additions & 0 deletions uniffi_bindgen/src/interface/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ impl ComponentInterface {
}
}
}

self.errors = self.errors
.drain()
.map(|mut name| {
visitor.visit_error_name(&mut name);
name
})
.collect()
}

fn fix_record_keys_after_rename(&mut self) {
Expand Down
5 changes: 5 additions & 0 deletions uniffi_bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ pub trait VisitMut {
/// naming conventions.
fn visit_type(&self, type_: &mut Type);

/// Go through each error name in the interface and adjust it to language specific naming
/// conventions. The new name must match the name of the Enum/Object definition after it's
/// visited.
fn visit_error_name(&self, name: &mut String);

/// Go through each `Method` of an `Object` and
/// adjust it to language specific naming conventions.
fn visit_method(&self, method: &mut Method);
Expand Down

0 comments on commit c682669

Please sign in to comment.