fix(cli): support sketches with custom main file names (#1329)

* fix(cli): support sketches with custom main file names

Previously, the CLI only accepted sketches where the main .pde file
matched the sketch folder name (e.g., sketch/sketch.pde). This caused
issues when users renamed their main file in the IDE, which stores
the custom filename in sketch.properties.

Now the CLI checks sketch.properties for a 'main' property before
falling back to the default naming convention, matching the IDE's
behavior implemented in Sketch.findMain().

Fixes #1219

* test: add CLI test for custom main file support

Added testSketchWithCustomMainFile() to CLITest.kt as requested by maintainer. This test provides a placeholder for manual testing of sketches with custom main files specified in sketch.properties.

Follows the same pattern as existing CLI tests (testLSP, testLegacyCLI) and is intended to be run manually in IntelliJ IDEA.

* test: convert to automated CLI test with temp directory

Converted testSketchWithCustomMainFile() from manual to automated test. Now creates a temporary sketch folder with custom main file and sketch.properties, then tests the CLI build command.

Follows the pattern from SchemaTest.kt using Files.createTempDirectory() and automatic cleanup.
This commit is contained in:
Avinash Kumar Deepak
2026-01-28 06:13:24 +05:30
committed by GitHub
parent 0a113f7c9e
commit fa85cd48e8
3 changed files with 139 additions and 1 deletions

View File

@@ -0,0 +1,89 @@
package processing.mode.java;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import processing.app.Settings;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import static org.junit.Assert.*;
public class CommanderTest {
private File tempSketchFolder;
@Before
public void setUp() throws IOException {
tempSketchFolder = Files.createTempDirectory("sketch_test").toFile();
}
@After
public void tearDown() {
if (tempSketchFolder != null && tempSketchFolder.exists()) {
deleteDirectory(tempSketchFolder);
}
}
@Test
public void testSketchWithDefaultMainFile() throws IOException {
String sketchName = tempSketchFolder.getName();
File mainFile = new File(tempSketchFolder, sketchName + ".pde");
try (FileWriter writer = new FileWriter(mainFile)) {
writer.write("void setup() {}\nvoid draw() {}");
}
assertTrue("Default main file should exist", mainFile.exists());
}
@Test
public void testSketchWithCustomMainFile() throws IOException {
File customMainFile = new File(tempSketchFolder, "custom_main.pde");
try (FileWriter writer = new FileWriter(customMainFile)) {
writer.write("void setup() {}\nvoid draw() {}");
}
File propsFile = new File(tempSketchFolder, "sketch.properties");
Settings props = new Settings(propsFile);
props.set("main", "custom_main.pde");
props.save();
assertTrue("Custom main file should exist", customMainFile.exists());
assertTrue("sketch.properties should exist", propsFile.exists());
Settings readProps = new Settings(propsFile);
assertEquals("custom_main.pde", readProps.get("main"));
}
@Test
public void testSketchPropertiesMainProperty() throws IOException {
File propsFile = new File(tempSketchFolder, "sketch.properties");
Settings props = new Settings(propsFile);
props.set("main", "my_sketch.pde");
props.save();
Settings readProps = new Settings(propsFile);
String mainFile = readProps.get("main");
assertEquals("Main property should match", "my_sketch.pde", mainFile);
}
private void deleteDirectory(File directory) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
directory.delete();
}
}