Skip to content

Commit cb3a7e0

Browse files
committed
review and implement tests for ${p_* macros
Signed-off-by: Christoph Rueger <chrisrueger@gmail.com>
1 parent b7c5734 commit cb3a7e0

File tree

8 files changed

+105
-88
lines changed

8 files changed

+105
-88
lines changed

biz.aQute.bndlib.tests/test/test/MacroTestsForDocsExamples.java

Lines changed: 98 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56
import static org.junit.jupiter.api.condition.OS.WINDOWS;
67

78
import java.io.File;
89
import java.io.IOException;
10+
import java.util.function.Consumer;
911

1012
import org.junit.jupiter.api.Disabled;
1113
import org.junit.jupiter.api.Test;
1214
import org.junit.jupiter.api.condition.DisabledOnOs;
1315
import org.junit.jupiter.api.condition.EnabledOnOs;
1416

17+
import aQute.bnd.build.Project;
1518
import aQute.bnd.build.Workspace;
1619
import aQute.bnd.header.Attrs;
1720
import aQute.bnd.osgi.Analyzer;
1821
import aQute.bnd.osgi.Builder;
1922
import aQute.bnd.osgi.Processor;
2023
import aQute.bnd.test.jupiter.InjectTemporaryDirectory;
24+
import aQute.lib.hex.Hex;
2125
import aQute.lib.io.IO;
2226

2327
/**
@@ -750,11 +754,9 @@ public void testDecorated() throws IOException {
750754
// ===== driver.md =====
751755
@Test
752756
public void testDriver() throws IOException {
753-
try (Processor p = new Processor()) {
754-
String result = p.getReplacer().process("${driver}");
755-
// Should return gradle, maven, or bnd depending on environment, or not expand
756-
assertThat(result).isNotNull();
757-
}
757+
// see test test.WorkspaceTest.testDriver()
758+
// we just keep it here so that we know there are tests for the examples
759+
// in the docs
758760
}
759761

760762
// ===== ee.md =====
@@ -929,11 +931,11 @@ public void testGestalt(@InjectTemporaryDirectory
929931
// ===== githead.md =====
930932
@Test
931933
public void testGithead() throws IOException {
932-
try (Processor p = new Processor()) {
933-
String result = p.getReplacer().process("${githead}");
934-
// May return empty if not in git repo, or a SHA if in git repo
935-
// Just check it doesn't throw
936-
assertThat(result).isNotNull();
934+
935+
try (Builder b = new Builder();) {
936+
String s = b.getReplacer()
937+
.process("${githead}");
938+
assertTrue(Hex.isHex(s));
937939
}
938940
}
939941

@@ -961,7 +963,7 @@ public void testGlobal() throws IOException {
961963
// global requires workspace context
962964
String result = p.getReplacer().process("${global;key}");
963965
// May not expand in test context
964-
assertThat(result).isNotNull();
966+
ensureDoesNotContainMacroLiteral(result);
965967
}
966968
}
967969

@@ -974,7 +976,7 @@ public void testIde() throws IOException {
974976
try (Processor p = new Processor()) {
975977
String result = p.getReplacer().process("${ide}");
976978
// Returns current IDE or empty
977-
assertThat(result).isNotNull();
979+
ensureDoesNotContainMacroLiteral(result);
978980
}
979981
}
980982

@@ -1129,72 +1131,85 @@ public void testNow() throws IOException {
11291131

11301132
// ===== p_allsourcepath.md =====
11311133
@Test
1132-
public void testPAllsourcepath() throws IOException {
1133-
try (Processor p = new Processor()) {
1134-
String result = p.getReplacer().process("${p_allsourcepath}");
1135-
// Returns project source paths - may be empty in test context
1136-
assertThat(result).isNotNull();
1137-
}
1134+
public void testPAllsourcepath(@InjectTemporaryDirectory
1135+
File tempDir) throws Exception {
1136+
assertWorkspaceAndProject(tempDir, "testresources/ws", "p-stale", p -> {
1137+
assertThat(p.getReplacer()
1138+
.process("${p_allsourcepath}")).containsAnyOf("p-stale/src", "p-stale-dep/src");
1139+
});
11381140
}
11391141

11401142
// ===== p_bootclasspath.md =====
1141-
@Test
1142-
public void testPBootclasspath() throws IOException {
1143-
try (Processor p = new Processor()) {
1144-
String result = p.getReplacer().process("${p_bootclasspath}");
1145-
// Returns project boot classpath - may be empty in test context
1146-
assertThat(result).isNotNull();
1147-
}
1143+
/**
1144+
* Don't know how to test this.
1145+
*/
1146+
@Disabled
1147+
public void testPBootclasspath() throws Exception {
1148+
// TODO don't know how to test setting the bootclasspath
11481149
}
11491150

11501151
// ===== p_buildpath.md =====
11511152
@Test
1152-
public void testPBuildpath() throws IOException {
1153-
try (Processor p = new Processor()) {
1154-
String result = p.getReplacer().process("${p_buildpath}");
1155-
// Returns project build path - may be empty in test context
1156-
assertThat(result).isNotNull();
1157-
}
1153+
public void testPBuildpath(@InjectTemporaryDirectory
1154+
File tempDir) throws Exception {
1155+
assertWorkspaceAndProject(tempDir, "testresources/ws", "p3", p -> {
1156+
assertThat(p.getReplacer()
1157+
.process("${p_buildpath}")).containsAnyOf("org.apache.felix.configadmin-1.0.1.jar");
1158+
});
1159+
11581160
}
11591161

11601162
// ===== p_dependson.md =====
11611163
@Test
1162-
public void testPDependson() throws IOException {
1163-
try (Processor p = new Processor()) {
1164-
String result = p.getReplacer().process("${p_dependson}");
1165-
// Returns projects this depends on - may be empty in test context
1166-
assertThat(result).isNotNull();
1167-
}
1164+
public void testPDependson(@InjectTemporaryDirectory
1165+
File tempDir) throws Exception {
1166+
assertWorkspaceAndProject(tempDir, "testresources/ws", "p-stale", p -> {
1167+
assertThat(p.getReplacer()
1168+
.process("${p_dependson}")).endsWith("p-stale-dep");
1169+
});
1170+
11681171
}
11691172

11701173
// ===== p_output.md =====
11711174
@Test
1172-
public void testPOutput() throws IOException {
1173-
try (Processor p = new Processor()) {
1174-
String result = p.getReplacer().process("${p_output}");
1175-
// Returns project output directory - may be empty in test context
1176-
assertThat(result).isNotNull();
1175+
public void tetstPOutput(@InjectTemporaryDirectory
1176+
File tempDir) throws Exception {
1177+
assertWorkspaceAndProject(tempDir, "testresources/ws-repo-test", "p1", p -> {
1178+
assertThat(p.getReplacer().process("${p_output}")).endsWith("p1/bin");
1179+
});
1180+
1181+
}
1182+
1183+
private void assertWorkspaceAndProject(File tempDir, String wspath, String project, Consumer<Project> s)
1184+
throws Exception {
1185+
1186+
IO.copy(IO.getFile(wspath), tempDir);
1187+
try (Workspace ws = new Workspace(tempDir)) {
1188+
Project p = ws.getProject(project);
1189+
s.accept(p);
11771190
}
11781191
}
11791192

11801193
// ===== p_sourcepath.md =====
11811194
@Test
1182-
public void testPSourcepath() throws IOException {
1183-
try (Processor p = new Processor()) {
1184-
String result = p.getReplacer().process("${p_sourcepath}");
1185-
// Returns project source path - may be empty in test context
1186-
assertThat(result).isNotNull();
1187-
}
1195+
public void testPSourcepath(@InjectTemporaryDirectory
1196+
File tempDir) throws Exception {
1197+
assertWorkspaceAndProject(tempDir, "testresources/ws-repo-test", "p1", p -> {
1198+
assertThat(p.getReplacer()
1199+
.process("${p_sourcepath}")).endsWith("p1/src");
1200+
});
11881201
}
11891202

11901203
// ===== p_testpath.md =====
11911204
@Test
1192-
public void testPTestpath() throws IOException {
1193-
try (Processor p = new Processor()) {
1194-
String result = p.getReplacer().process("${p_testpath}");
1195-
// Returns project test path - may be empty in test context
1196-
assertThat(result).isNotNull();
1197-
}
1205+
public void testPTestpath(@InjectTemporaryDirectory
1206+
File tempDir) throws Exception {
1207+
assertWorkspaceAndProject(tempDir, "testresources/ws", "multipath", p -> {
1208+
// just test for some jars
1209+
assertThat(p.getReplacer()
1210+
.process("${p_testpath}")).containsAnyOf("org.apache.felix.configadmin-1.8.8.jar",
1211+
"org.apache.felix.ipojo-1.0.0.jar");
1212+
});
11981213
}
11991214

12001215
// ===== path.md =====
@@ -1230,12 +1245,9 @@ public void testPathseparatorUnix() throws IOException {
12301245
// ===== permissions.md =====
12311246
@Test
12321247
public void testPermissions() throws IOException {
1233-
try (Processor p = new Processor()) {
1234-
p.setProperty("Import-Package", "org.osgi.framework");
1235-
String result = p.getReplacer().process("${permissions}");
1236-
// Returns permission declarations
1237-
assertThat(result).isNotNull();
1238-
}
1248+
// see tests in PermissionGeneratorTest
1249+
// we just keep it here so that we know there are tests for the examples
1250+
// in the docs
12391251
}
12401252

12411253
// ===== propertiesdir.md =====
@@ -1244,7 +1256,7 @@ public void testPropertiesdir() throws IOException {
12441256
try (Processor p = new Processor()) {
12451257
String result = p.getReplacer().process("${propertiesdir}");
12461258
// Returns directory containing bnd properties file
1247-
assertThat(result).isNotNull();
1259+
ensureDoesNotContainMacroLiteral(result);
12481260
}
12491261
}
12501262

@@ -1254,7 +1266,7 @@ public void testPropertiesname() throws IOException {
12541266
try (Processor p = new Processor()) {
12551267
String result = p.getReplacer().process("${propertiesname}");
12561268
// Returns name of bnd properties file
1257-
assertThat(result).isNotNull();
1269+
ensureDoesNotContainMacroLiteral(result);
12581270
}
12591271
}
12601272

@@ -1289,22 +1301,29 @@ public void testRemoveall() throws IOException {
12891301
}
12901302

12911303
// ===== repodigests.md =====
1292-
@Test
1293-
public void testRepodigests() throws IOException {
1294-
try (Processor p = new Processor()) {
1295-
String result = p.getReplacer().process("${repodigests}");
1296-
// Returns repository content digests - may be empty in test context
1297-
assertThat(result).isNotNull();
1298-
}
1304+
/**
1305+
* Don't know how to test this.
1306+
*/
1307+
@Disabled
1308+
public void testRepodigests(@InjectTemporaryDirectory
1309+
File tempDir) throws Exception {
1310+
// TODO come up with a test, but have not found no class implementing
1311+
// RepositoryDigest interface
1312+
12991313
}
13001314

13011315
// ===== repos.md =====
13021316
@Test
1303-
public void testRepos() throws IOException {
1304-
try (Processor p = new Processor()) {
1305-
String result = p.getReplacer().process("${repos}");
1317+
public void testRepos(@InjectTemporaryDirectory
1318+
File tempDir) throws Exception {
1319+
IO.copy(IO.getFile("testresources/ws-repo-test"), tempDir);
1320+
try (Workspace ws = new Workspace(tempDir)) {
1321+
Project p = ws.getProject("p1");
1322+
String result = p.getReplacer()
1323+
.process("${repos}");
13061324
// Returns list of repositories - may be empty in test context
1307-
assertThat(result).isNotNull();
1325+
ensureDoesNotContainMacroLiteral(result);
1326+
assertThat(result).containsAnyOf("bnd-cache", "Release", "Repo");
13081327
}
13091328
}
13101329

@@ -1408,7 +1427,7 @@ public void testSystemAllowFail() throws IOException {
14081427

14091428
// Execute command that fails - should not throw
14101429
result = p.getReplacer().process("${system_allow_fail;false}");
1411-
assertThat(result).isNotNull();
1430+
ensureDoesNotContainMacroLiteral(result);
14121431
}
14131432
}
14141433

@@ -1518,5 +1537,10 @@ public void testWorkspace(@InjectTemporaryDirectory
15181537

15191538
}
15201539

1540+
private void ensureDoesNotContainMacroLiteral(String result) {
1541+
assertThat(result).isNotNull()
1542+
.doesNotContain("${");
1543+
}
1544+
15211545
// Additional tests can be added here for other macros
15221546
}

docs/_macros/p_allsourcepath.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ All-Sources: ${p_allsourcepath}
4747
- Returns absolute paths
4848
- See also: `${p_sourcepath}` for project sources only
4949

50-
<hr />
51-
TODO Needs review - AI Generated content
50+
5251

5352
---
5453

docs/_macros/p_bootclasspath.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ Boot-Classpath: ${p_bootclasspath}
4848
- See also: `${p_buildpath}` for project dependencies
4949

5050

51-
<hr />
52-
TODO Needs review - AI Generated content
51+
5352

5453
---
5554

docs/_macros/p_buildpath.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ dependency.count=${size;${split;,;${p_buildpath}}}
5353
- See also: `${p_testpath}` for test dependencies
5454
- See also: `${p_dependson}` for project dependencies
5555

56-
<hr />
57-
TODO Needs review - AI Generated content
56+
5857

5958
---
6059

docs/_macros/p_dependson.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ Depends-On: ${p_dependson}
4747
- See also: `${p_buildpath}` for JAR dependencies
4848

4949

50-
<hr />
51-
TODO Needs review - AI Generated content
50+
5251

5352
---
5453

docs/_macros/p_output.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ classes.location=${p_output}/classes
5252
- See also: `${p_sourcepath}` for source directories
5353
- See also: `${basedir}` for project root
5454

55-
<hr />
56-
TODO Needs review - AI Generated content
55+
5756

5857
---
5958

docs/_macros/p_sourcepath.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ Source-Directories: ${p_sourcepath}
4848
- See also: `${p_output}` for output directory
4949

5050

51-
<hr />
52-
TODO Needs review - AI Generated content
51+
5352

5453
---
5554

docs/_macros/p_testpath.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ Test-Dependencies: ${p_testpath}
4848
- See also: `${p_buildpath}` for compile dependencies
4949

5050

51-
<hr />
52-
TODO Needs review - AI Generated content
51+
5352

5453
---
5554

0 commit comments

Comments
 (0)