-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot/github_actions/ossf/scorecard-act…
…ion-2.3.1
- Loading branch information
Showing
18 changed files
with
202 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
picocli-examples/src/main/java/picocli/examples/stdin/PrintFirstLine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package picocli.examples.stdin; | ||
|
||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Option; | ||
import picocli.CommandLine.Parameters; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* This example reads a file, or from standard input, and prints the first line. | ||
* | ||
* It follows the UNIX convention that tools should read from STDIN (standard input) | ||
* when the end user specifies the `-` character instead of a file name. | ||
* | ||
* See POSIX Utility Syntax Guidelines, Guideline 13: | ||
* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02 | ||
*/ | ||
@Command(name = "firstline") | ||
public class PrintFirstLine implements Callable<Integer> { | ||
|
||
@Parameters(arity = "1..*") | ||
List<File> files; | ||
|
||
@Option(names = "--charset", description = "Charset of the file (or STDIN) to read. Default: ${DEFAULT_VALUE}") | ||
Charset charset = Charset.defaultCharset(); | ||
|
||
public Integer call() throws Exception { | ||
for (File file : files) { | ||
printFirstLine(file); | ||
} | ||
return 0; | ||
} | ||
|
||
private void printFirstLine(File file) throws IOException { | ||
try (BufferedReader reader = createReader(file)) { | ||
String line = reader.readLine(); | ||
System.out.println(line); | ||
} | ||
} | ||
|
||
private BufferedReader createReader(File file) throws IOException { | ||
InputStream in = "-".equals(file.toString()) | ||
? System.in | ||
: Files.newInputStream(file.toPath()); | ||
return new BufferedReader(new InputStreamReader(in, charset)); | ||
} | ||
|
||
public static void main(String... args) { | ||
int exitCode = new CommandLine(new PrintFirstLine()).execute(args); | ||
//System.exit(exitCode); // prevents this method from being testable... | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
picocli-examples/src/test/java/picocli/examples/stdin/PrintFirstLineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package picocli.examples.stdin; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.contrib.java.lang.system.SystemOutRule; | ||
import org.junit.contrib.java.lang.system.TextFromStandardInputStream; | ||
|
||
import java.net.URL; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class PrintFirstLineTest { | ||
|
||
@Rule | ||
public final SystemOutRule outRule = new SystemOutRule().enableLog().muteForSuccessfulTests(); | ||
|
||
@Rule | ||
public final TextFromStandardInputStream systemInMock = TextFromStandardInputStream.emptyStandardInputStream(); | ||
|
||
@Test | ||
public void testMain() { | ||
URL resource = PrintFirstLineTest.class.getResource("/PrintFirstLineTest.txt"); | ||
String path = resource.getPath(); | ||
PrintFirstLine.main(path); | ||
assertEquals("file line 1", outRule.getLog().trim()); | ||
} | ||
|
||
@Test | ||
public void testStandardInput() { | ||
systemInMock.provideLines("stdin line1", "stdin line2", "stdin line3"); | ||
PrintFirstLine.main("-"); | ||
assertEquals("stdin line1", outRule.getLog().trim()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
file line 1 | ||
file line 2 | ||
file line 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Picocli Java 8 Tests | ||
|
||
This subproject contains tests that use Java 8, and the JUnit 5 and System Lambda test frameworks. | ||
|
||
This module does not publish any artifacts. | ||
|
||
NOTE: only put tests here that require Java 8 and cannot be run in earlier versions of Java. | ||
|
||
Tests that require Java 9 or later should be located in the `picocli-tests-java9plus` subproject. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
group 'info.picocli' | ||
description 'Picocli Tests Requiring Java 8' | ||
version "$projectVersion" | ||
|
||
sourceCompatibility = 1.8 | ||
targetCompatibility = 1.8 | ||
|
||
test { | ||
useJUnitPlatform() | ||
} | ||
|
||
|
||
dependencies { | ||
api rootProject | ||
testImplementation supportDependencies.junit5Api | ||
testRuntimeOnly supportDependencies.junit5Engine | ||
testImplementation supportDependencies.systemLambda | ||
} | ||
|
||
jar { | ||
manifest { | ||
attributes 'Specification-Title': 'Picocli Tests Requiring Java 8', | ||
'Specification-Vendor' : 'Remko Popma', | ||
'Specification-Version' : archiveVersion.get(), | ||
'Implementation-Title' : 'Picocli Tests Requiring Java 8', | ||
'Implementation-Vendor' : 'Remko Popma', | ||
'Implementation-Version': archiveVersion.get(), | ||
'Automatic-Module-Name' : 'info.picocli.tests.java8' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package picocli; | ||
|
||
import org.junit.Test; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.*; | ||
|
||
import java.io.File; | ||
import java.util.Optional; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class Issue2232 { | ||
static class Tar { | ||
@Option(names = { "-f", "--file" }, paramLabel = "ARCHIVE", description = "the archive file") | ||
Optional<File> archive; | ||
|
||
public Tar() { | ||
archive = Optional.of(new File("helloworld")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDefault() { | ||
Tar tar = new Tar(); | ||
System.out.println(tar.archive); | ||
assertEquals(Optional.of(new File("helloworld")), tar.archive); | ||
new CommandLine(tar).parseArgs(); | ||
assertEquals(Optional.of(new File("helloworld")), tar.archive); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters