Skip to content

Commit e73ae53

Browse files
committed
ci: add GitHub workflow for Maven tests
1 parent 706e9ff commit e73ae53

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

.github/workflows/test.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Compile
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
build-and-deploy:
12+
runs-on: ubuntu-latest
13+
environment: prod
14+
15+
steps:
16+
# Checkout the repository
17+
- uses: actions/checkout@v4
18+
19+
# Set up JDK 21
20+
- name: Set up JDK 24
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: '24'
24+
distribution: 'temurin'
25+
cache: maven
26+
cache-dependency-path: pom.xml
27+
28+
# Build with Maven
29+
- name: Compile with Maven
30+
run: mvn test -f pom.xml
31+

src/test/java/MavenTest.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.time.Duration;
1111
import java.util.Arrays;
1212
import java.util.Collection;
13-
import java.util.Collections; // For emptyList()
13+
import java.util.Collections;
1414
import java.util.List;
1515
import java.util.concurrent.Callable;
1616
import java.util.concurrent.ExecutionException;
@@ -19,45 +19,54 @@
1919
import java.util.concurrent.Future;
2020
import java.util.concurrent.TimeUnit;
2121
import java.util.concurrent.TimeoutException;
22+
import java.util.regex.Pattern; // Import Pattern
2223
import java.util.stream.Collectors;
2324
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.DynamicTest;
2526
import org.junit.jupiter.api.TestFactory;
2627

2728
public class MavenTest {
2829

29-
// Removed the hardcoded PROBLEMS list
30-
3130
// Define the number of threads for the ExecutorService
3231
private static final int MAX_THREADS = 10;
3332
// Define the timeout for each test
3433
private static final Duration TEST_TIMEOUT = Duration.ofSeconds(3);
3534

35+
// Regex pattern to match "p" followed by only digits
36+
private static final Pattern PROBLEM_DIR_PATTERN = Pattern.compile("p\\d+");
37+
3638
// Method to dynamically discover problem names
3739
private static List<String> discoverProblemNames() {
38-
// The base package for UVA solutions
3940
String basePackagePath = "com/lzw/solutions/uva/";
4041
Path uvaSolutionsPath = null;
4142

4243
try {
43-
// Get the URL for the package directory within the classpath
44-
// This works whether running from IDE or jar
4544
URL resource = Thread.currentThread().getContextClassLoader().getResource(basePackagePath);
4645
if (resource == null) {
4746
System.err.println("Could not find resource path: " + basePackagePath);
4847
return Collections.emptyList();
4948
}
5049

51-
// Convert URL to Path, handling different URL schemes (e.g., jar:file:/...)
52-
// For simple file system, this is usually enough
50+
// Handle JAR paths correctly. If it's in a JAR, resource.toURI() will be "jar:file:/..."
51+
// We need to resolve the actual file system path if running from an unzipped structure,
52+
// or return an empty list if it's purely in a JAR and cannot be listed as a directory.
53+
if ("jar".equals(resource.getProtocol())) {
54+
System.err.println(
55+
"Cannot discover problems dynamically from within a JAR file. Please ensure 'src/main/java' is accessible on the file system during testing.");
56+
// In a real scenario, you might have a pre-defined list for JAR runs,
57+
// or expect tests to be run against expanded directories.
58+
return Collections.emptyList();
59+
}
60+
5361
uvaSolutionsPath = Paths.get(resource.toURI());
62+
5463
} catch (URISyntaxException e) {
5564
System.err.println("Error converting resource URL to URI: " + e.getMessage());
5665
return Collections.emptyList();
5766
}
5867

5968
if (uvaSolutionsPath == null || !Files.exists(uvaSolutionsPath)) {
60-
System.err.println("UVA Solutions directory not found: " + uvaSolutionsPath);
69+
System.err.println("UVA Solutions directory not found or not a directory: " + uvaSolutionsPath);
6170
return Collections.emptyList();
6271
}
6372

@@ -66,8 +75,9 @@ private static List<String> discoverProblemNames() {
6675
File[] problemDirs = uvaDir.listFiles(new FilenameFilter() {
6776
@Override
6877
public boolean accept(File current, String name) {
69-
// Ensure it's a directory and starts with 'p' (e.g., p100, p140)
70-
return new File(current, name).isDirectory() && name.startsWith("p");
78+
// Ensure it's a directory AND matches the "p" + digits pattern
79+
return new File(current, name).isDirectory()
80+
&& PROBLEM_DIR_PATTERN.matcher(name).matches();
7181
}
7282
});
7383

0 commit comments

Comments
 (0)