ManualPrepareCommand.java

1
package org.egothor.methodatlas.command;
2
3
import java.io.IOException;
4
import java.io.PrintWriter;
5
import java.nio.file.Path;
6
import java.nio.file.Paths;
7
import java.util.ArrayList;
8
import java.util.LinkedHashMap;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.logging.Level;
12
import java.util.logging.Logger;
13
import java.util.stream.Collectors;
14
15
import org.egothor.methodatlas.CliConfig;
16
import org.egothor.methodatlas.ManualMode;
17
import org.egothor.methodatlas.ai.AiSuggestionException;
18
import org.egothor.methodatlas.ai.ManualPrepareEngine;
19
import org.egothor.methodatlas.ai.PromptBuilder;
20
import org.egothor.methodatlas.api.DiscoveredMethod;
21
import org.egothor.methodatlas.api.TestDiscovery;
22
import org.egothor.methodatlas.api.TestDiscoveryConfig;
23
24
/**
25
 * CLI command handler for the {@code -manual-prepare} mode.
26
 *
27
 * <p>
28
 * Discovers test classes via the configured {@link TestDiscovery} providers,
29
 * then writes one work file per class to the prepare work directory.
30
 * No CSV output is produced; only progress lines are written to the
31
 * supplied {@link PrintWriter}.
32
 * </p>
33
 *
34
 * @see org.egothor.methodatlas.ai.ManualPrepareEngine
35
 * @see org.egothor.methodatlas.ai.ManualConsumeEngine
36
 */
37
public final class ManualPrepareCommand implements Command {
38
39
    private static final Logger LOG = Logger.getLogger(ManualPrepareCommand.class.getName());
40
41
    private final ManualMode.Prepare prepare;
42
    private final CliConfig cliConfig;
43
    private final TestDiscoveryConfig discoveryConfig;
44
    private final PluginLoader pluginLoader;
45
46
    /**
47
     * Creates a new manual-prepare command.
48
     *
49
     * @param prepare         manual prepare mode configuration (work and response dirs)
50
     * @param cliConfig       full parsed CLI configuration
51
     * @param discoveryConfig discovery configuration forwarded to providers
52
     * @param pluginLoader    loader used to resolve {@link TestDiscovery}
53
     *                        providers from the classpath
54
     */
55
    public ManualPrepareCommand(ManualMode.Prepare prepare, CliConfig cliConfig,
56
            TestDiscoveryConfig discoveryConfig, PluginLoader pluginLoader) {
57
        this.prepare = prepare;
58
        this.cliConfig = cliConfig;
59
        this.discoveryConfig = discoveryConfig;
60
        this.pluginLoader = pluginLoader;
61
    }
62
63
    /**
64
     * Discovers test classes, writes work files, and reports progress.
65
     *
66
     * @param out writer for progress and summary output
67
     * @return {@code 0} if all files were processed successfully, {@code 1} if any
68
     *         provider encountered a processing error
69
     * @throws IOException if traversing a file tree fails
70
     */
71
    @Override
72
    @SuppressWarnings("PMD.CloseResource") // providers closed by closeAll() in the finally block
73
    public int execute(PrintWriter out) throws IOException {
74
        ManualPrepareEngine engine;
75
        try {
76
            engine = new ManualPrepareEngine(prepare.workDir(), prepare.responseDir(), cliConfig.aiOptions());
77
        } catch (AiSuggestionException e) {
78
            throw new IllegalStateException("Failed to initialize manual prepare engine", e);
79
        }
80
81 2 1. execute : removed conditional - replaced equality check with false → SURVIVED
2. execute : removed conditional - replaced equality check with true → KILLED
        List<Path> roots = cliConfig.paths().isEmpty() ? List.of(Paths.get(".")) : cliConfig.paths();
82
        List<TestDiscovery> providers = pluginLoader.loadProviders(discoveryConfig);
83
        boolean hadErrors = false;
84
        int prepared = 0;
85
86
        try {
87
            for (Path root : roots) {
88
                List<DiscoveredMethod> allMethods = new ArrayList<>(); // NOPMD – intentionally one list per scan root
89
                for (TestDiscovery provider : providers) {
90 1 1. execute : removed call to java/util/stream/Stream::forEach → KILLED
                    provider.discover(root).forEach(allMethods::add);
91 2 1. execute : removed conditional - replaced equality check with true → KILLED
2. execute : removed conditional - replaced equality check with false → KILLED
                    if (provider.hadErrors()) {
92
                        hadErrors = true;
93
                    }
94
                }
95
96
                // Group by FQCN so each class produces one work file.
97
                Map<String, List<DiscoveredMethod>> byClass = allMethods.stream()
98
                        .collect(Collectors.groupingBy(DiscoveredMethod::fqcn,
99
                                LinkedHashMap::new, Collectors.toList()));
100
101
                for (Map.Entry<String, List<DiscoveredMethod>> entry : byClass.entrySet()) {
102
                    String fqcn = entry.getKey();
103
                    List<DiscoveredMethod> classMethods = entry.getValue();
104
                    String classSource = classMethods.get(0).sourceContent().get().orElse(null);
105 2 1. execute : removed conditional - replaced equality check with false → SURVIVED
2. execute : removed conditional - replaced equality check with true → KILLED
                    if (classSource == null) {
106
                        continue;
107
                    }
108
                    String fileStem = classMethods.get(0).fileStem();
109
                    List<PromptBuilder.TargetMethod> targetMethods = classMethods.stream()
110
                            .map(ScanOrchestrator::toTargetMethod).toList();
111
                    try {
112
                        Path workFile = engine.prepare(fileStem, fqcn, classSource, targetMethods);
113 1 1. execute : removed call to java/io/PrintWriter::println → KILLED
                        out.println("Prepared: " + workFile);
114 1 1. execute : Changed increment from 1 to -1 → SURVIVED
                        prepared++;
115
                    } catch (AiSuggestionException e) {
116
                        if (LOG.isLoggable(Level.WARNING)) {
117
                            LOG.log(Level.WARNING, "Failed to prepare work file for " + fqcn, e);
118
                        }
119
                    }
120
                }
121
            }
122
        } finally {
123 1 1. execute : removed call to org/egothor/methodatlas/command/PluginLoader::closeAll → SURVIVED
            pluginLoader.closeAll(providers);
124
        }
125
126 1 1. execute : removed call to java/io/PrintWriter::println → KILLED
        out.println("Manual prepare complete. Wrote " + prepared + " work file(s) to " + prepare.workDir()
127
                + " (response stubs in " + prepare.responseDir() + ")");
128 2 1. execute : removed conditional - replaced equality check with false → KILLED
2. execute : removed conditional - replaced equality check with true → KILLED
        return hadErrors ? 1 : 0;
129
    }
130
}

Mutations

81

1.1
Location : execute
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

2.2
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppScanCoverageTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppScanCoverageTest]/[method:manualPrepare_unparseableFile_returnsExitCode1(java.nio.file.Path)]
removed conditional - replaced equality check with true → KILLED

90

1.1
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppManualTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppManualTest]/[method:manualPrepare_createsWorkFilesForAllDiscoveredTestClasses(java.nio.file.Path)]
removed call to java/util/stream/Stream::forEach → KILLED

91

1.1
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppManualTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppManualTest]/[method:manualPrepare_returnsZeroExitCodeOnSuccess(java.nio.file.Path)]
removed conditional - replaced equality check with true → KILLED

2.2
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppScanCoverageTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppScanCoverageTest]/[method:manualPrepare_unparseableFile_returnsExitCode1(java.nio.file.Path)]
removed conditional - replaced equality check with false → KILLED

105

1.1
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppManualTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppManualTest]/[method:manualPrepare_createsWorkFilesForAllDiscoveredTestClasses(java.nio.file.Path)]
removed conditional - replaced equality check with true → KILLED

2.2
Location : execute
Killed by : none
removed conditional - replaced equality check with false → SURVIVED
Covering tests

113

1.1
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppManualTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppManualTest]/[method:manualPrepare_createsWorkFilesForAllDiscoveredTestClasses(java.nio.file.Path)]
removed call to java/io/PrintWriter::println → KILLED

114

1.1
Location : execute
Killed by : none
Changed increment from 1 to -1 → SURVIVED
Covering tests

123

1.1
Location : execute
Killed by : none
removed call to org/egothor/methodatlas/command/PluginLoader::closeAll → SURVIVED
Covering tests

126

1.1
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppScanCoverageTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppScanCoverageTest]/[method:manualPrepare_classWithNoTestMethods_writesNoWorkFiles(java.nio.file.Path)]
removed call to java/io/PrintWriter::println → KILLED

128

1.1
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppScanCoverageTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppScanCoverageTest]/[method:manualPrepare_unparseableFile_returnsExitCode1(java.nio.file.Path)]
removed conditional - replaced equality check with false → KILLED

2.2
Location : execute
Killed by : org.egothor.methodatlas.MethodAtlasAppManualTest.[engine:junit-jupiter]/[class:org.egothor.methodatlas.MethodAtlasAppManualTest]/[method:manualPrepare_returnsZeroExitCodeOnSuccess(java.nio.file.Path)]
removed conditional - replaced equality check with true → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1