Repository navigation
feat(package-api): let Package tests run Runtime Scripts and source scans without exemptions #629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #![cfg_attr(not(has_packages), allow(dead_code, unused_imports))] | ||
|
|
||
| //! Steps a Package's own unit tests need. The root exists only under `cfg(test)`, and the Package | ||
| //! API lint accepts it only in a Package file gated on `#![cfg(test)]`. | ||
|
|
||
| use crate::runtime_script::{ask_event, EffectSink, RuntimeScriptHost, ScriptEvent}; | ||
|
|
||
| pub(crate) use crate::runtime_script::{EntityView, RuntimeScript}; | ||
| pub(crate) use crate::test_scan::{code_of, read_scanned, shape_of}; | ||
|
|
||
| /// Run `scripts` in order for one event on a fresh Runtime Script Host and return the Script | ||
| /// Answer, read as `script_binding::ask` reads it. Staged Effects are discarded. Any Script | ||
| /// Diagnostic is an error, one diagnostic per line. | ||
| pub(crate) fn ask_offline( | ||
| event: &str, | ||
| actor: Option<EntityView>, | ||
| target: Option<EntityView>, | ||
| scripts: &[RuntimeScript<'_>], | ||
| ) -> Result<Option<f64>, String> { | ||
| let event = ScriptEvent { | ||
| name: event.to_string(), | ||
| actor, | ||
| target, | ||
| }; | ||
| let (diagnostics, answer) = ask_event( | ||
| &mut RuntimeScriptHost::new(), | ||
| &mut NoEffects, | ||
| &event, | ||
| scripts, | ||
| ); | ||
| if diagnostics.is_empty() { | ||
| Ok(answer) | ||
| } else { | ||
| Err(diagnostics | ||
| .iter() | ||
| .map(ToString::to_string) | ||
| .collect::<Vec<_>>() | ||
| .join("\n")) | ||
| } | ||
| } | ||
|
|
||
| struct NoEffects; | ||
|
|
||
| impl EffectSink for NoEffects { | ||
| fn grant_xp(&mut self, _character_guid: u64, _amount: u32) {} | ||
| fn heal(&mut self, _healer_guid: u64, _target_guid: u64, _amount: u32) {} | ||
| fn send_chat(&mut self, _recipient_guid: u64, _message: &str) {} | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn script(source: &str) -> RuntimeScript<'_> { | ||
| RuntimeScript { | ||
| name: "probe", | ||
| source, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_offline_ask_returns_the_first_answer() { | ||
| let scripts = [ | ||
| script("return nil"), | ||
| script("return 15"), | ||
| script("return 40"), | ||
| ]; | ||
| assert_eq!( | ||
| ask_offline("pkg.flee_at", None, None, &scripts), | ||
| Ok(Some(15.0)) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_offline_ask_refuses_a_failing_script() { | ||
| let failure = ask_offline("pkg.flee_at", None, None, &[script("error('broken')")]) | ||
| .expect_err("a raising script is a diagnostic"); | ||
| assert!( | ||
| failure.contains("runtime script `probe` on `pkg.flee_at`"), | ||
| "{failure}" | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: LyraCoreProject/LyraCore
Length of output: 3826
🏁 Script executed:
Repository: LyraCoreProject/LyraCore
Length of output: 44783
Exclude test-only files from ordinary registry discovery.
registry_file_enabledfilters only debug-reducer files. A#![cfg(test)]Package file can therefore be scanned, and its marker path is emitted intopackage_registries.rswithout a test gate. Rust removes the marked item from an ordinary build, so the generated reference can fail compilation.🐛 Suggested fix
fn registry_file_enabled(source: &str, debug_reducers: bool) -> bool { - debug_reducers || !debug_only_file(source) + file_gate(source) != Some(TEST_FILE_CFG) + && (debug_reducers || !debug_only_file(source)) }🤖 Prompt for AI Agents