Skip to content

Commit

Permalink
Feature/kotlin 1.9.10 (#27)
Browse files Browse the repository at this point in the history
* Kotlin upgraded to v1.9.10
* Fixed unit tests for kotlin and java engines
* Fix unit tests on windows
* Fix surefire plugin socket factory for unit tests
  • Loading branch information
jjlauer authored Oct 27, 2023
1 parent b8740a5 commit fdf4707
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 177 deletions.
59 changes: 59 additions & 0 deletions blaze-core/src/main/java/com/fizzed/blaze/util/BlazeRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fizzed.blaze.util;

import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class BlazeRunner {

static public ProcessResult invokeWithCurrentJvmHome(File blazeScriptFile, List<String> blazeArgs, List<String> scriptArgs) throws IOException, InterruptedException, TimeoutException {
return invokeWithCurrentJvmHome(blazeScriptFile, blazeArgs, scriptArgs, null);
}

static public ProcessResult invokeWithCurrentJvmHome(File blazeScriptFile, List<String> blazeArgs, List<String> scriptArgs, File workingDirectory) throws IOException, InterruptedException, TimeoutException {
// get current classpath
String classpath = System.getProperty("java.class.path");

// build a temporary home directory (that classes will be compiled to)
//Path tempHomeDir = Paths.get(System.getProperty("java.io.tmpdir")).resolve("blaze-test-"+ UUID.randomUUID());
//Files.createDirectories(tempHomeDir);
try {
final List<String> commands = new ArrayList<>();

commands.add("java");
commands.add("-cp");
commands.add(classpath);
commands.add(com.fizzed.blaze.cli.Bootstrap.class.getCanonicalName());

if (blazeArgs != null) {
commands.addAll(blazeArgs);
}

if (blazeScriptFile != null) {
commands.add(blazeScriptFile.getAbsolutePath());
}

if (scriptArgs != null) {
commands.addAll(scriptArgs);
}

return new ProcessExecutor()
.command(commands)
.redirectOutput(System.out)
.readOutput(true)
.directory(workingDirectory)
//.environment("HOME", tempHomeDir.toAbsolutePath().toString())
.timeout(10, TimeUnit.SECONDS)
.execute();
} finally {
//Systems.remove(tempHomeDir).recursive().force().run();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class EngineHelperTest {

@Test @Ignore("service loader does return same instance")
public void newInstanceReturned() {
Engine engine0 = EngineHelper.findByFileExtension(".js", false);
Engine engine0 = EngineHelper.findByFileExtension(".java", false);

Engine engine1 = EngineHelper.findByFileExtension(".js", false);
Engine engine1 = EngineHelper.findByFileExtension(".java", false);

assertThat(engine0, not(sameInstance(engine1)));
}
Expand Down
131 changes: 50 additions & 81 deletions blaze-core/src/test/java/com/fizzed/blaze/jdk/BlazeJdkEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,30 @@
package com.fizzed.blaze.jdk;

import com.fizzed.blaze.Context;
import com.fizzed.blaze.core.Blaze;
import com.fizzed.blaze.core.BlazeTask;
import com.fizzed.blaze.internal.ConfigHelper;
import com.fizzed.blaze.internal.ContextImpl;
import static com.fizzed.blaze.system.ShellTestHelper.getBinDirAsResource;
import static com.fizzed.blaze.internal.FileHelper.resourceAsPath;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import com.fizzed.blaze.util.BlazeRunner;
import org.apache.commons.io.FileUtils;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.exec.ProcessResult;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.fizzed.blaze.internal.FileHelper.resourceAsFile;
import static com.fizzed.blaze.system.ShellTestHelper.getBinDirAsResource;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
*
* @author joelauer
*/
public class BlazeJdkEngineTest {
final static private Logger log = LoggerFactory.getLogger(BlazeJdkEngineTest.class);

Expand All @@ -63,82 +59,55 @@ static public void clearCache() throws IOException {
FileUtils.deleteDirectory(classesDir.toFile());
}

@Test @Ignore
@Test
public void hello() throws Exception {
Blaze blaze = new Blaze.Builder()
.file(resourceAsPath("/jdk/hello.java"))
.build();

systemOutRule.clearLog();

blaze.execute();

assertThat(systemOutRule.getLog(), containsString("Hello World!"));
final File scriptFile = resourceAsFile("/jdk/hello.java");

final ProcessResult result = BlazeRunner.invokeWithCurrentJvmHome(scriptFile, null, null);

assertThat(result.getExitValue(), is(0));
assertThat(result.outputUTF8(), containsString("Hello World!" + System.lineSeparator()));
}

@Test @Ignore
@Test
public void tasks() throws Exception {
Blaze blaze = new Blaze.Builder()
.file(resourceAsPath("/jdk/only_public.java"))
.build();

systemOutRule.clearLog();

List<BlazeTask> tasks = blaze.tasks();

assertThat(tasks, hasSize(1));
assertThat(tasks.get(0).getName(), is("main"));
final File scriptFile = resourceAsFile("/jdk/only_public.java");

final ProcessResult result = BlazeRunner.invokeWithCurrentJvmHome(scriptFile, asList("-l"), null);

assertThat(result.getExitValue(), is(0));
assertThat(result.outputUTF8(), containsString("tasks =>" + System.lineSeparator() + " main"));
}

@Test @Ignore
@Test
public void defaultBlazeInWorkingDir() throws Exception {
Blaze blaze = new Blaze.Builder()
.directory(resourceAsPath("/jdk/project0"))
.build();

systemOutRule.clearLog();

blaze.execute();

assertThat(systemOutRule.getLog(), containsString("worked"));

assertThat(blaze.context().scriptFile(), is(resourceAsPath("/jdk/project0/blaze.java")));
assertThat(blaze.context().baseDir(), is(resourceAsPath("/jdk/project0")));
assertThat(blaze.context().withBaseDir("test"), is(resourceAsPath("/jdk/project0").resolve("test")));
final File workingDir = resourceAsFile("/jdk/project0");
final File scriptFile = resourceAsFile("/jdk/project0/blaze.java");

final ProcessResult result = BlazeRunner.invokeWithCurrentJvmHome(scriptFile, null, null, workingDir);

assertThat(result.getExitValue(), is(0));
assertThat(result.outputUTF8(), containsString("worked" + System.lineSeparator()));
}

@Test @Ignore
@Test
public void defaultBlazeInSubBlazeDir() throws Exception {
Blaze blaze = new Blaze.Builder()
.directory(resourceAsPath("/jdk/project1"))
.build();

systemOutRule.clearLog();

blaze.execute();

assertThat(systemOutRule.getLog(), containsString("worked"));

assertThat(blaze.context().scriptFile(), is(resourceAsPath("/jdk/project1/blaze/blaze.java")));
assertThat(blaze.context().baseDir(), is(resourceAsPath("/jdk/project1/blaze")));
assertThat(blaze.context().withBaseDir("../test"), is(resourceAsPath("/jdk/project1").resolve("test")));
final File workingDir = resourceAsFile("/jdk/project1");

final ProcessResult result = BlazeRunner.invokeWithCurrentJvmHome(null, null, null, workingDir);

assertThat(result.getExitValue(), is(0));
assertThat(result.outputUTF8(), containsString("worked" + System.lineSeparator()));
}

@Test @Ignore
@Test
public void defaultBlazeInSubDotBlazeDir() throws Exception {
Blaze blaze = new Blaze.Builder()
.directory(resourceAsPath("/jdk/project2"))
.build();

systemOutRule.clearLog();

blaze.execute();

assertThat(systemOutRule.getLog(), containsString("worked"));

assertThat(blaze.context().scriptFile(), is(resourceAsPath("/jdk/project2/.blaze/blaze.js")));
assertThat(blaze.context().baseDir(), is(resourceAsPath("/jdk/project2/.blaze")));
assertThat(blaze.context().withBaseDir("../test"), is(resourceAsPath("/jdk/project2").resolve("test")));
final File workingDir = resourceAsFile("/jdk/project2");

final ProcessResult result = BlazeRunner.invokeWithCurrentJvmHome(null, null, null, workingDir);

assertThat(result.getExitValue(), is(0));
assertThat(result.outputUTF8(), containsString("worked" + System.lineSeparator()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
*
* @author joelauer
*/
public class TryIt {
static private final Logger log = LoggerFactory.getLogger(TryIt.class);
public class DemoMain {
static private final Logger log = LoggerFactory.getLogger(DemoMain.class);

static public void main(String[] args) throws Exception {
File scriptFile = FileHelper.resourceAsFile("/jdk/hello.java");
Expand Down
3 changes: 2 additions & 1 deletion blaze-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-runtime</artifactId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>

<!-- testing -->

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
package com.fizzed.blaze.kotlin;

import java.util.concurrent.atomic.AtomicInteger;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation;
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity;
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation;
import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
import org.slf4j.Logger;

Expand Down Expand Up @@ -46,7 +50,7 @@ public int getWarnings() {
return warnings.get();
}

@Override
/*@Override
public void report(CompilerMessageSeverity severity, String message, CompilerMessageLocation location) {
switch (severity) {
case INFO:
Expand All @@ -62,6 +66,33 @@ public void report(CompilerMessageSeverity severity, String message, CompilerMes
this.logger.error("{} @ {}", message, location);
break;
}
}*/

@Override
public void clear() {

}

@Override
public boolean hasErrors() {
return false;
}

@Override
public void report(@NotNull CompilerMessageSeverity severity, @NotNull String message, @Nullable CompilerMessageSourceLocation location) {
switch (severity) {
case INFO:
this.logger.info("{} @ {}", message, location);
break;
case WARNING:
this.warnings.incrementAndGet();
this.logger.warn("{} @ {}", message, location);
break;
case ERROR:
case EXCEPTION:
this.errors.incrementAndGet();
this.logger.error("{} @ {}", message, location);
break;
}
}

}
Loading

0 comments on commit fdf4707

Please sign in to comment.