Skip to content

Commit

Permalink
testCommandWhenNoADRsCreated
Browse files Browse the repository at this point in the history
  • Loading branch information
adoble committed Oct 21, 2024
1 parent 595d43a commit f3ac1ac
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
54 changes: 52 additions & 2 deletions src/test/java/org/doble/adr/CommandGenerateTocTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

import org.hamcrest.Matcher;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -222,8 +224,56 @@ void testCommandWithNonExistingTemplateFile() {
}

@Test
void testCommandWhenNoADRsCreated() {
fail("TO DO");
void testCommandWhenNoADRsCreated() throws Exception {

// Remove all ADRS
Path adrPath = tempDir.resolve("project/doc/adr");
TestUtilities.removeAllFilesInDirectory(adrPath);

// Create a template for test
String testTemplateContent = """
# ADR files
Test empty TOC
## Entries
{{#entries}}
* ADR {{id}} : {{filename}}
{{/entries}}
End of entries
""";

Path testTemplatePath = tempDir.resolve(templatesPath).resolve("test_template.md");
Files.createFile(testTemplatePath);
Files.writeString(testTemplatePath, testTemplateContent);

String[] args = { "generate", "toc", "-t", testTemplatePath.toString() };

int exitCode = ADR.run(args, env);
assertEquals(0, exitCode);

Path tocPath = tempDir.resolve("project/doc/adr/toc.md");

// Check if the TOC file has been created
assertTrue(Files.exists(tocPath));

// Sample check the expected contents
String expectedSample1 = "Test empty TOC";
String actual = Files.readString(tocPath);

assert (actual.contains(expectedSample1));

// Use a regex to extract the content between the string "##Entries" and "End of
// entries"
String regex = "## Entries(.*?)End of entries";
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex, java.util.regex.Pattern.DOTALL);
java.util.regex.Matcher matcher = pattern.matcher(actual);

if (matcher.find()) {
assertEquals("", matcher.group(1).trim()); // Extract the content between the markers
} else {
fail("TOC entries is not empty: " + matcher.group(1).trim());
}

}

}
39 changes: 36 additions & 3 deletions src/test/java/org/doble/adr/TestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class TestUtilities {

Expand Down Expand Up @@ -179,4 +179,37 @@ public static String trimContent(String input) {

return result.toString();
}

/**
*
* Removes all files (and NOT directories) in a specified directory.
* The function throws an exception if the provided path is not a valid
* directory.
*/

public static void removeAllFilesInDirectory(Path directory) throws IOException {

// Ensure the directory exists
if (!Files.isDirectory(directory)) {
throw new IllegalArgumentException("The provided path is not a directory.");
}

// Use Files.walkFileTree to iterate over the files
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// Delete each file
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
// Handle file visit failure (e.g., permission issues)
System.err.println("Failed to access file: " + file + " due to " + exc.getMessage());
return FileVisitResult.CONTINUE;
}
});
}

}

0 comments on commit f3ac1ac

Please sign in to comment.