Do not validate .model.json files with no content/whitespace only (#420)

* Ignore empty/whitespace-only model.json files

* Ignore no return value from model.json files during snapshot

* Use str::from_utf8 instead of String::from_utf8

* Revert "Ignore no return value from model.json files during snapshot"

This reverts commit 0aef16e30a.

* Add test for empty .model.json files

* Change empty .model.json check method

Co-authored-by: Lucien Greathouse <me@lpghatguy.com>

* Format code with cargo fmt

* Use raw string instead

Co-authored-by: Lucien Greathouse <me@lpghatguy.com>
This commit is contained in:
Mixu78
2021-05-14 23:16:05 +03:00
committed by GitHub
parent 7599ef6626
commit ca8865a3ce
8 changed files with 130 additions and 2 deletions

View File

@@ -0,0 +1,28 @@
---
source: tests/tests/serve.rs
expression: "read_response.intern_and_redact(&mut redactions, root_id)"
---
instances:
id-2:
Children:
- id-3
ClassName: Folder
Id: id-2
Metadata:
ignoreUnknownInstances: false
Name: empty_folder
Parent: "00000000000000000000000000000000"
Properties: {}
id-3:
Children: []
ClassName: Model
Id: id-3
Metadata:
ignoreUnknownInstances: false
Name: test
Parent: id-2
Properties: {}
messageCursor: 1
sessionId: id-1

View File

@@ -0,0 +1,18 @@
---
source: tests/tests/serve.rs
expression: "read_response.intern_and_redact(&mut redactions, root_id)"
---
instances:
id-2:
Children: []
ClassName: Folder
Id: id-2
Metadata:
ignoreUnknownInstances: false
Name: empty_folder
Parent: "00000000000000000000000000000000"
Properties: {}
messageCursor: 0
sessionId: id-1

View File

@@ -0,0 +1,14 @@
---
source: tests/tests/serve.rs
expression: redactions.redacted_yaml(info)
---
expectedPlaceIds: ~
gameId: ~
placeId: ~
projectName: empty_folder
protocolVersion: 4
rootInstanceId: id-2
serverVersion: "[server-version]"
sessionId: id-1

View File

@@ -0,0 +1,21 @@
---
source: tests/tests/serve.rs
expression: "subscribe_response.intern_and_redact(&mut redactions, ())"
---
messageCursor: 1
messages:
- added:
id-3:
Children: []
ClassName: Model
Id: id-3
Metadata:
ignoreUnknownInstances: false
Name: test
Parent: id-2
Properties: {}
removed: []
updated: []
sessionId: id-1

View File

@@ -0,0 +1,6 @@
{
"name": "empty_folder",
"tree": {
"$path": "src"
}
}

View File

@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashMap, path::Path};
use std::{borrow::Cow, collections::HashMap, path::Path, str};
use anyhow::Context;
use memofs::Vfs;
@@ -18,7 +18,14 @@ pub fn snapshot_json_model(
instance_name: &str,
) -> SnapshotInstanceResult {
let contents = vfs.read(path)?;
let instance: JsonModel = serde_json::from_slice(&contents)
let contents_str = str::from_utf8(&contents)
.with_context(|| format!("File was not valid UTF-8: {}", path.display()))?;
if contents_str.trim().is_empty() {
return Ok(None);
}
let instance: JsonModel = serde_json::from_str(contents_str)
.with_context(|| format!("File is not a valid JSON model: {}", path.display()))?;
let mut snapshot = instance

View File

@@ -186,3 +186,37 @@ fn move_folder_of_stuff() {
);
});
}
#[test]
fn empty_json_model() {
run_serve_test("empty_json_model", |session, mut redactions| {
let info = session.get_api_rojo().unwrap();
let root_id = info.root_instance_id;
assert_yaml_snapshot!("empty_json_model_info", redactions.redacted_yaml(info));
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"empty_json_model_all",
read_response.intern_and_redact(&mut redactions, root_id)
);
fs::write(
session.path().join("src/test.model.json"),
r#"{"ClassName": "Model"}"#,
)
.unwrap();
let subscribe_response = session.get_api_subscribe(0).unwrap();
assert_yaml_snapshot!(
"empty_json_model_subscribe",
subscribe_response.intern_and_redact(&mut redactions, ())
);
let read_response = session.get_api_read(root_id).unwrap();
assert_yaml_snapshot!(
"empty_json_model_all-2",
read_response.intern_and_redact(&mut redactions, root_id)
);
});
}