mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Reload Zed schemas after they are regenerated
Zed's JSON server caches schema files until its configuration changes and never reports schema file edits. Schema files are now written through one writer that hands back the text, and a hash of those texts goes into the Zed settings as irisSchemaHash, so regenerated schemas reach the editor. v3: f31d41a9f
This commit is contained in:
1 parent
ffbd0db03a
commit
fc3ea69d74
9 files changed
+355
-121
No files matched your search
@@ -143,16 +143,23 @@ public class IrisCodeWorkspaceTest {
|
||||
JSONArray schemas = assertWorkspaceSchemasMaterialized(pack, canonicalWorkspace);
|
||||
assertTrue("Studio open must give Zed the same schemas",
|
||||
Files.readString(pack.toPath().resolve(".zed/settings.json"), StandardCharsets.UTF_8).contains(schemas.getJSONObject(0).getString("url")));
|
||||
String firstSchemaHash = zedSchemaHash(pack);
|
||||
String firstSchemaPath = schemas.getJSONObject(0).getString("url").substring(2);
|
||||
Files.writeString(canonicalWorkspace.toPath(), "{\"settings\":{\"json.schemas\":[]}}", StandardCharsets.UTF_8);
|
||||
Files.delete(new File(pack, firstSchemaPath).toPath());
|
||||
|
||||
workspace.prepareAndOpenEditor();
|
||||
assertWorkspaceSchemasMaterialized(pack, canonicalWorkspace);
|
||||
assertEquals("Regenerating identical schemas must keep Zed's schema hash", firstSchemaHash, zedSchemaHash(pack));
|
||||
assertEquals("Studio open must target only the canonical workspace", "unrelated",
|
||||
Files.readString(unrelatedWorkspace.toPath(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String zedSchemaHash(File pack) throws Exception {
|
||||
JSONObject settings = new JSONObject(Files.readString(pack.toPath().resolve(".zed/settings.json"), StandardCharsets.UTF_8));
|
||||
return settings.getJSONObject("lsp").getJSONObject("json-language-server").getJSONObject("settings").getString("irisSchemaHash");
|
||||
}
|
||||
|
||||
private static JSONArray assertWorkspaceSchemasMaterialized(File pack, File canonicalWorkspace) throws Exception {
|
||||
JSONObject configuration = new JSONObject(Files.readString(canonicalWorkspace.toPath(), StandardCharsets.UTF_8));
|
||||
JSONArray schemas = configuration.getJSONObject("settings").getJSONArray("json.schemas");
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package art.arcane.iris.core.project;
|
||||
|
||||
import art.arcane.iris.core.IrisSettings;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
public class SchemaFilesTest {
|
||||
private static final String SCHEMA_TEXT = "{\"type\": \"object\"}";
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private SchemaTestPlatform platform;
|
||||
private IrisSettings previousSettings;
|
||||
|
||||
@Before
|
||||
public void bindPlatform() throws Exception {
|
||||
platform = SchemaTestPlatform.bind(temporaryFolder.newFolder("platform"));
|
||||
previousSettings = IrisSettings.settings;
|
||||
IrisSettings.settings = new IrisSettings();
|
||||
}
|
||||
|
||||
@After
|
||||
public void restorePlatform() {
|
||||
IrisSettings.settings = previousSettings;
|
||||
platform.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void immediateWriteReturnsTheTextItWrote() throws Exception {
|
||||
File schemaFile = new File(temporaryFolder.getRoot(), ".iris/schema/biome-schema.json");
|
||||
|
||||
String text = SchemaFiles.write(schemaFile, () -> SCHEMA_TEXT, true).join();
|
||||
|
||||
assertEquals(SCHEMA_TEXT, text);
|
||||
assertEquals(SCHEMA_TEXT, Files.readString(schemaFile.toPath(), StandardCharsets.UTF_8).stripTrailing());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void immediateWriteThrowsWhenTheSchemaCannotBeBuilt() {
|
||||
File schemaFile = new File(temporaryFolder.getRoot(), "broken-schema.json");
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> SchemaFiles.write(schemaFile, () -> {
|
||||
throw new IllegalStateException("broken");
|
||||
}, true));
|
||||
assertFalse(schemaFile.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void backgroundWriteReturnsTheTextItWrote() throws Exception {
|
||||
File schemaFile = new File(temporaryFolder.getRoot(), "region-schema.json");
|
||||
|
||||
String text = SchemaFiles.write(schemaFile, () -> SCHEMA_TEXT, false).join();
|
||||
|
||||
assertEquals(SCHEMA_TEXT, text);
|
||||
assertEquals(SCHEMA_TEXT, Files.readString(schemaFile.toPath(), StandardCharsets.UTF_8).stripTrailing());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failedBackgroundWriteHashesAsEmptyText() {
|
||||
File schemaFile = new File(temporaryFolder.getRoot(), "broken-schema.json");
|
||||
|
||||
String text = SchemaFiles.write(schemaFile, () -> {
|
||||
throw new IllegalStateException("broken");
|
||||
}, false).join();
|
||||
|
||||
assertEquals(SchemaFiles.FAILED_SCHEMA_TEXT, text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void awaitTextsKeepsOrderAndMapsFailuresToEmptyText() {
|
||||
CompletableFuture<String> failed = new CompletableFuture<>();
|
||||
failed.completeExceptionally(new IllegalStateException("broken"));
|
||||
|
||||
List<String> texts = SchemaFiles.awaitTexts(List.of(CompletableFuture.completedFuture("a"), failed, CompletableFuture.completedFuture("c")));
|
||||
|
||||
assertEquals(List.of("a", SchemaFiles.FAILED_SCHEMA_TEXT, "c"), texts);
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,16 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ZedProjectSettingsTest {
|
||||
private static final String BIOME_SCHEMA_TEXT = "{\"enum\": [\"land/plains\"]}";
|
||||
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@@ -26,7 +31,7 @@ public class ZedProjectSettingsTest {
|
||||
public void createsSettingsFileWithSchemasWhenMissing() throws Exception {
|
||||
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||
|
||||
zed.writeIrisSettings(biomeSchemas());
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||
|
||||
assertTrue("Zed settings must be created", Files.isRegularFile(zed.settingsFile()));
|
||||
JsonObject settings = readSettings(zed.settingsFile());
|
||||
@@ -52,7 +57,7 @@ public class ZedProjectSettingsTest {
|
||||
}
|
||||
""", StandardCharsets.UTF_8);
|
||||
|
||||
zed.writeIrisSettings(biomeSchemas());
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||
|
||||
JsonObject settings = readSettings(zed.settingsFile());
|
||||
assertEquals(JsonParser.parseString("[\"*.jsonc5\", \"json\"]"), jsoncGlobsIn(settings));
|
||||
@@ -78,7 +83,7 @@ public class ZedProjectSettingsTest {
|
||||
""";
|
||||
Files.writeString(zed.settingsFile(), jsonc, StandardCharsets.UTF_8);
|
||||
|
||||
zed.writeIrisSettings(biomeSchemas());
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||
|
||||
assertEquals(jsonc, Files.readString(zed.settingsFile(), StandardCharsets.UTF_8));
|
||||
}
|
||||
@@ -86,18 +91,58 @@ public class ZedProjectSettingsTest {
|
||||
@Test
|
||||
public void unchangedSchemasDoNotRewriteTheFile() throws Exception {
|
||||
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||
zed.writeIrisSettings(biomeSchemas());
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||
Path file = zed.settingsFile();
|
||||
String first = Files.readString(file, StandardCharsets.UTF_8);
|
||||
FileTime stamp = FileTime.fromMillis(Files.getLastModifiedTime(file).toMillis() - 60_000L);
|
||||
Files.setLastModifiedTime(file, stamp);
|
||||
|
||||
zed.writeIrisSettings(biomeSchemas());
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||
|
||||
assertEquals(first, Files.readString(file, StandardCharsets.UTF_8));
|
||||
assertEquals("Unchanged Zed settings must not be rewritten", stamp.toMillis(), Files.getLastModifiedTime(file).toMillis());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void changedSchemaContentChangesTheSchemaHash() throws Exception {
|
||||
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT));
|
||||
String firstHash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of(BIOME_SCHEMA_TEXT.replace("land/plains", "land/meadow")));
|
||||
|
||||
String secondHash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||
assertNotEquals("A regenerated schema must change Zed's JSON server configuration", firstHash, secondHash);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void schemaHashIsAStableSixteenDigitHexOfTheTexts() throws Exception {
|
||||
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of("a", "b"));
|
||||
String hash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of("a", "b"));
|
||||
|
||||
assertTrue(hash.matches("[0-9a-f]{16}"));
|
||||
assertEquals(hash, schemaHashIn(readSettings(zed.settingsFile())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void textBoundariesChangeTheSchemaHash() throws Exception {
|
||||
ZedProjectSettings zed = new ZedProjectSettings(temporaryFolder.newFolder("pack"));
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of("ab", "c"));
|
||||
String firstHash = schemaHashIn(readSettings(zed.settingsFile()));
|
||||
|
||||
zed.writeIrisSettings(biomeSchemas(), List.of("a", "bc"));
|
||||
|
||||
assertNotEquals(firstHash, schemaHashIn(readSettings(zed.settingsFile())));
|
||||
}
|
||||
|
||||
private static String schemaHashIn(JsonObject settings) {
|
||||
return settings.getAsJsonObject("lsp").getAsJsonObject("json-language-server")
|
||||
.getAsJsonObject("settings").get("irisSchemaHash").getAsString();
|
||||
}
|
||||
|
||||
private static JSONArray biomeSchemas() {
|
||||
JSONObject entry = new JSONObject();
|
||||
entry.put("fileMatch", new JSONArray(new Object[]{"/biomes/*.json", "/biomes/*/*.json"}));
|
||||
|
||||
Reference in new issue
Block a user