Skip to content

Commit

Permalink
+ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Nov 15, 2024
1 parent 0167373 commit 69b8385
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 30 deletions.
54 changes: 24 additions & 30 deletions src/main/java/org/cqfn/astranaut/cli/Generate.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,20 @@ public void perform(final Program program, final List<String> args) throws BaseE
this.basepkg
);
info.setVersion(this.options.getVersion());
String path =
new File(this.root.toString(), "package-info.java").getAbsolutePath();
String code = info.generateJavaCode();
boolean result = new FilesWriter(path).writeStringNoExcept(code);
if (!result) {
throw new CannotWriteFile(path);
}
Generate.writeFile(
new File(this.root.toString(), "package-info.java"),
info.generateJavaCode()
);
final Context.Constructor cct = new Context.Constructor();
cct.setLicense(this.license);
cct.setPackage(this.basepkg);
cct.setVersion(this.options.getVersion());
final Context context = cct.createContext();
final CompilationUnit provider = new FactoryProviderGenerator(program).createUnit(context);
code = provider.generateJavaCode();
path = new File(this.root.toString(), provider.getFileName()).getAbsolutePath();
result = new FilesWriter(path).writeStringNoExcept(code);
if (!result) {
throw new CannotWriteFile(path);
}
Generate.writeFile(
new File(this.root.toString(), provider.getFileName()),
provider.generateJavaCode()
);
}

/**
Expand All @@ -141,37 +136,36 @@ private void generateNodes(final Program program, final String language) throws
}
final PackageInfo info = new PackageInfo(this.license, brief, pkg);
info.setVersion(this.options.getVersion());
String path = new File(folder, "package-info.java").getAbsolutePath();
boolean result = new FilesWriter(path).writeStringNoExcept(info.generateJavaCode());
if (!result) {
throw new CannotWriteFile(path);
}
Generate.writeFile(new File(folder, "package-info.java"), info.generateJavaCode());
final Context.Constructor cct = new Context.Constructor();
cct.setLicense(this.license);
cct.setPackage(pkg);
cct.setVersion(this.options.getVersion());
final Context context = cct.createContext();
final CompilationUnit factory = this.factories.createUnit(language, context);
String code = factory.generateJavaCode();
path = new File(folder, factory.getFileName()).getAbsolutePath();
result = new FilesWriter(path).writeStringNoExcept(code);
if (!result) {
throw new CannotWriteFile(path);
}
Generate.writeFile(new File(folder, factory.getFileName()), factory.generateJavaCode());
for (final NodeDescriptor rule : program.getNodeDescriptorsForLanguage(language)) {
final RuleGenerator generator = rule.createGenerator();
final Set<CompilationUnit> units = generator.createUnits(context);
for (final CompilationUnit unit : units) {
code = unit.generateJavaCode();
path = new File(folder, unit.getFileName()).getAbsolutePath();
result = new FilesWriter(path).writeStringNoExcept(code);
if (!result) {
throw new CannotWriteFile(path);
}
Generate.writeFile(new File(folder, unit.getFileName()), unit.generateJavaCode());
}
}
}

/**
* Writes a file.
* @param file File
* @param content File content
* @throws CliException In case it is not possible to write a file.
*/
private static void writeFile(final File file, final String content) throws CliException {
final boolean result = new FilesWriter(file.getAbsolutePath()).writeStringNoExcept(content);
if (!result) {
throw new CannotWriteFile(file.getName());
}
}

/**
* Exception 'Cannot write file'.
* @since 1.0.0
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/org/cqfn/astranaut/cli/GenerateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
*/
package org.cqfn.astranaut.cli;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Locale;
import java.util.Set;
import org.cqfn.astranaut.core.utils.FilesWriter;
import org.cqfn.astranaut.exceptions.BaseException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -33,6 +39,7 @@
* End-to-end tests checking file generation for different rule sets.
* @since 1.0.0
*/
@SuppressWarnings("PMD.TooManyMethods")
class GenerateTest extends EndToEndTest {
@Test
void nodeWithoutChildren(final @TempDir Path temp) {
Expand Down Expand Up @@ -92,6 +99,57 @@ void readBadDsl(final @TempDir Path temp) {
);
}

@Test
void writeToProtectedFile(final @TempDir Path temp) {
boolean oops = false;
try {
final Path dir = temp.resolve("output");
final Path readonly = temp.resolve("output/org/cqfn/uast/tree/package-info.java");
new FilesWriter(readonly.toFile().getAbsolutePath()).writeStringNoExcept("xxx");
if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win")) {
Files.setAttribute(readonly, "dos:readonly", true);
} else {
final Set<PosixFilePermission> permissions =
Files.getPosixFilePermissions(readonly);
permissions.remove(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(dir, permissions);
}
String message = "";
final String[] args = {
"generate",
"src/test/resources/dsl/node_without_children.dsl",
"--output",
dir.toFile().getAbsolutePath(),
"--package",
"org.cqfn.uast.tree",
"--license",
"LICENSE.txt",
"--version",
"1.0.0",
};
boolean wow = false;
try {
Main.run(args);
} catch (final BaseException exception) {
wow = true;
message = exception.getErrorMessage();
}
if (System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win")) {
Files.setAttribute(readonly, "dos:readonly", false);
} else {
final Set<PosixFilePermission> permissions =
Files.getPosixFilePermissions(readonly);
permissions.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(dir, permissions);
}
Assertions.assertTrue(wow);
Assertions.assertEquals("Cannot write file: 'package-info.java'", message);
} catch (final IOException ignored) {
oops = true;
}
Assertions.assertFalse(oops);
}

/**
* Runs the project in code generation mode and compiles all generated files
* into a single listing.
Expand Down

0 comments on commit 69b8385

Please sign in to comment.