If a test has variants, do not add the base test class because that will just cause...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / multivm / VMTestFrameworkDetector.java
blobe4d6ca0a3adc81bb57ef525ef2c88ecf14b0a2b7
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.multivm;
12 import cc.squirreljme.plugin.util.TestDetection;
13 import java.io.File;
14 import java.util.HashSet;
15 import java.util.Map;
16 import java.util.Set;
17 import lombok.Setter;
18 import org.gradle.api.internal.file.RelativeFile;
19 import org.gradle.api.internal.tasks.testing.DefaultTestClassRunInfo;
20 import org.gradle.api.internal.tasks.testing.TestClassProcessor;
21 import org.gradle.api.internal.tasks.testing.detection.TestFrameworkDetector;
23 /**
24 * Detects tests in the framework.
26 * @since 2022/09/11
28 public class VMTestFrameworkDetector
29 implements TestFrameworkDetector
31 /** The test classes within. */
32 @Setter
33 protected Set<File> testClasses;
35 /** The classpath to use for tests. */
36 @Setter
37 protected Set<File> testClasspath;
39 /** The tests which are available. */
40 protected final Map<String, CandidateTestFiles> tests;
42 /**
43 * Initializes the framework detector.
45 * @param __tests The tests that are available.
46 * @throws NullPointerException On null arguments.
47 * @since 2022/09/11
49 public VMTestFrameworkDetector(Map<String, CandidateTestFiles> __tests)
50 throws NullPointerException
52 if (__tests == null)
53 throw new NullPointerException("NARG");
55 this.tests = __tests;
58 /**
59 * {@inheritDoc}
60 * @since 2022/09/11
62 @Override
63 public void startDetection(TestClassProcessor __processor)
65 // Go through the available tests and register each one individually
66 Set<String> didBaseTest = new HashSet<>();
67 for (String test : this.tests.keySet())
69 // If this is a variant of a test, include the base test so
70 // Gradle actually knows it exists
71 int atSign = test.lastIndexOf('@');
72 if (atSign >= 0)
74 String baseTest = test.substring(0, atSign);
76 // If not added already, put it in
77 if (!didBaseTest.contains(baseTest))
79 __processor.processTestClass(
80 new DefaultTestClassRunInfo(baseTest));
81 didBaseTest.add(baseTest);
85 // Add test
86 else
87 __processor.processTestClass(
88 new DefaultTestClassRunInfo(test));
92 /**
93 * This is asking if the given test is something that would be run by
94 * this framework, in which case TAC is simple and is just prefixes and
95 * such.
97 * The actual output of this we probably do not really care for at all.
99 * @param __testClassFile The test class.
100 * @return If this is a test.
101 * @since 2022/09/11
103 @Override
104 public boolean processTestClass(RelativeFile __testClassFile)
106 // This will give for example io/MarkableInputStreamTest.class
107 String path = __testClassFile.getRelativePath().getPathString();
109 // Remove the suffix if there is any
110 if (path.endsWith(".class"))
111 path = path.substring(0, path.length() - ".class".length());
113 // If there is an at sign in this test then remove it since
114 int lastSlash = path.lastIndexOf('/');
115 int atSign = path.lastIndexOf('@');
116 if (atSign >= 0 && atSign > lastSlash)
117 path = path.substring(0, atSign);
119 // Remove the suffix if there is any, after @ removal since it may
120 // potentially appear twice
121 if (path.endsWith(".class"))
122 path = path.substring(0, path.length() - ".class".length());
124 // Normalize name for SquirrelJME, then check if this is something
125 // we care about
126 return TestDetection.isTest(path.replace('/', '.'));