Skip to content

Commit

Permalink
Merge pull request #3680 from objectionary/3678
Browse files Browse the repository at this point in the history
more careful package name reading
  • Loading branch information
yegor256 authored Dec 16, 2024
2 parents 80f6b0b + c562dbd commit d92811a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private int javaGenerated(final boolean rewrite, final Path target, final String
final Path tgt = new Place(jname).make(
this.generatedDir.toPath(), TranspileMojo.JAVA
);
this.pinfo(tgt, jname, java.xpath("@package").get(0));
this.pinfo(tgt, jname, java.xpath("@package").stream().findFirst().orElse(""));
final Supplier<Path> che = new CachePath(
this.cache.toPath().resolve(TranspileMojo.CACHE),
this.plugin.getVersion(),
Expand Down Expand Up @@ -322,10 +322,14 @@ private int javaGenerated(final boolean rewrite, final Path target, final String
private void pinfo(final Path java, final String oname, final String pname)
throws IOException {
final Path pinfo = java.getParent().resolve("package-info.java");
if (!pinfo.toFile().exists()) {
if (!pinfo.toFile().exists() && !pname.isEmpty()) {
if (pinfo.getParent().toFile().mkdirs()) {
Logger.debug(this, "Directory created for %[file]s", pinfo);
}
String pkg = oname;
if (oname.contains(".")) {
pkg = pkg.substring(0, pkg.lastIndexOf('.'));
}
Files.write(
pinfo,
String.join(
Expand All @@ -335,7 +339,7 @@ private void pinfo(final Path java, final String oname, final String pname)
" * don't modify it, all changes will be lost anyway.",
" */",
String.format("// @org.eolang.XmirPackage(\"%s\")", pname),
String.format("package %s;", oname.substring(0, oname.lastIndexOf('.')))
String.format("package %s;", pkg)
).getBytes(StandardCharsets.UTF_8)
);
Logger.debug(this, "Saved %[file]s (%[size]s)", pinfo, pinfo.toFile().length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import com.yegor256.Mktmp;
import com.yegor256.MktmpResolver;
import com.yegor256.farea.Farea;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -54,6 +56,7 @@
*/
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
@ExtendWith(MktmpResolver.class)
@ExtendWith(RandomProgramResolver.class)
final class TranspileMojoTest {

/**
Expand Down Expand Up @@ -82,6 +85,74 @@ void createsPreStylesheets(final String yaml) {
);
}

@Test
void transpilesWithPackage(@Mktmp final Path temp)
throws Exception {
new Farea(temp).together(
f -> {
f.clean();
f.files().file("src/main/eo/one/foo.eo").write(
String.join(
"\n",
"+package one",
"",
"# no comments.",
"[] > foo",
" QQ.io.stdout > @",
" \"Hello, world!\\n\"",
""
).getBytes(StandardCharsets.UTF_8)
);
f.build()
.plugins()
.appendItself()
.execution()
.goals("register", "parse", "optimize", "shake", "transpile");
f.exec("process-sources");
}
);
MatcherAssert.assertThat(
"the .java file is generated",
temp.resolve("target/generated-sources/EOone/EOfoo.java").toFile().exists(),
Matchers.is(true)
);
MatcherAssert.assertThat(
"the package-info.java file contains the right package name",
Files.readString(
temp.resolve("target/generated-sources/EOone/package-info.java"),
StandardCharsets.UTF_8
),
Matchers.containsString("package EOone;")
);
}

@Test
void transpilesSimpleApp(@Mktmp final Path temp, @RandomProgram final String prog)
throws Exception {
new Farea(temp).together(
f -> {
f.clean();
f.files().file("src/main/eo/foo.eo").write(prog.getBytes());
f.build()
.plugins()
.appendItself()
.execution()
.goals("register", "parse", "optimize", "shake", "transpile");
f.exec("process-sources");
}
);
MatcherAssert.assertThat(
"the .java file is re-generated",
temp.resolve("target/generated-sources/EOfoo.java").toFile().exists(),
Matchers.is(true)
);
MatcherAssert.assertThat(
"the package-info.java file contains the right package name",
temp.resolve("target/generated-sources/package-info.java").toFile().exists(),
Matchers.is(false)
);
}

@Test
void recompilesIfModified(@Mktmp final Path temp) throws IOException {
final FakeMaven maven = new FakeMaven(temp);
Expand Down

0 comments on commit d92811a

Please sign in to comment.