|
| 1 | +/* |
| 2 | + * (c) Copyright 2026 Palantir Technologies Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.palantir.javaformat.java; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import com.palantir.javaformat.java.JavaFormatterOptions.Style; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.parallel.Execution; |
| 24 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.CsvSource; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
| 28 | + |
| 29 | +/** |
| 30 | + * JBang reads its directives from the line comments before the first line of code, and a shell runs the first line of |
| 31 | + * a script, so those lines are left exactly as written. The goldens {@code ojf-issue-24-jbang-*} show whole scripts, |
| 32 | + * with the same directives after the first line of code formatted like any other comment. |
| 33 | + */ |
| 34 | +@Execution(ExecutionMode.CONCURRENT) |
| 35 | +final class JBangDirectivesTest { |
| 36 | + |
| 37 | + private static final Formatter FORMATTER = Formatter.createFormatter( |
| 38 | + JavaFormatterOptions.builder().style(Style.OJF).build()); |
| 39 | + |
| 40 | + @ParameterizedTest |
| 41 | + @ValueSource( |
| 42 | + strings = { |
| 43 | + // Every name JBang knows. |
| 44 | + "//CDS", |
| 45 | + "//COMPILE_OPTIONS -Xlint:all", |
| 46 | + "//DEPS info.picocli:picocli:4.7.6", |
| 47 | + "//DESCRIPTION Prints a greeting", |
| 48 | + "//DOCS guide=./readme.md", |
| 49 | + "//FILES application.properties", |
| 50 | + "//GAV org.example:hello:1.0", |
| 51 | + "//GROOVY 3.0.19", |
| 52 | + "//JAVA 21+", |
| 53 | + "//JAVAAGENT myagent.jar=option1,option2", |
| 54 | + "//JAVAC_OPTIONS -parameters", |
| 55 | + "//JAVA_OPTIONS -Xmx512m", |
| 56 | + "//KOTLIN 2.0.21", |
| 57 | + "//MAIN org.example.Hello", |
| 58 | + "//MANIFEST Built-By=jbang", |
| 59 | + "//MODULE org.example.hello", |
| 60 | + "//NATIVE_OPTIONS --no-fallback", |
| 61 | + "//NOINTEGRATIONS", |
| 62 | + "//PREVIEW", |
| 63 | + "//REPOS central,jitpack", |
| 64 | + "//RUNTIME_OPTIONS -XX:+UseSerialGC", |
| 65 | + "//SOURCES Helper.java", |
| 66 | + // A directive for a build integration: Quarkus reads //Q:CONFIG. |
| 67 | + "//Q:CONFIG quarkus.banner.enabled=false", |
| 68 | + // What JBang's syntax allows after the name. |
| 69 | + "//DEPS\tinfo.picocli:picocli:4.7.6", |
| 70 | + "//DEPS info.picocli:picocli:4.7.6 // parses the command line", |
| 71 | + "//DEPS org.postgresql:postgresql:${env.DB_VERSION:42.6.0}", |
| 72 | + "//DEPS", |
| 73 | + // Longer than the 120 columns at which a line comment is wrapped. |
| 74 | + "//DEPS com.fasterxml.jackson.core:jackson-databind:2.17.2 org.slf4j:slf4j-simple:2.0.13" |
| 75 | + + " com.squareup.okhttp3:okhttp:4.12.0", |
| 76 | + }) |
| 77 | + void keepsDirectiveBeforeFirstLineOfCode(String directive) throws FormatterException { |
| 78 | + String script = directive + "\n\nclass Hello {}\n"; |
| 79 | + assertThat(FORMATTER.formatSource(script)).isEqualTo(script); |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @CsvSource( |
| 84 | + delimiterString = " => ", |
| 85 | + value = { |
| 86 | + // Not a name JBang knows. |
| 87 | + "//DEPSSS a:b:1 => // DEPSSS a:b:1", |
| 88 | + "//DEPS_ a:b:1 => // DEPS_ a:b:1", |
| 89 | + "//TODO pin the versions => // TODO pin the versions", |
| 90 | + // No whitespace after the name. |
| 91 | + "//JAVA21+ => // JAVA21+", |
| 92 | + "//DEPS:a:b:1 => // DEPS:a:b:1", |
| 93 | + // Directive names are upper case. |
| 94 | + "//deps a:b:1 => // deps a:b:1", |
| 95 | + "//Deps a:b:1 => // Deps a:b:1", |
| 96 | + // Three slashes, as in a Markdown doc comment. |
| 97 | + "///DEPS a:b:1 => /// DEPS a:b:1", |
| 98 | + // Switched off on purpose: JBang's own templates list optional dependencies this way. |
| 99 | + "// DEPS a:b:1 => // DEPS a:b:1", |
| 100 | + "// //DEPS a:b:1 => // //DEPS a:b:1", |
| 101 | + }) |
| 102 | + void formatsLookalikeAsOrdinaryComment(String comment, String expected) throws FormatterException { |
| 103 | + assertThat(FORMATTER.formatSource(comment + "\n\nclass Hello {}\n")) |
| 104 | + .isEqualTo(expected + "\n\nclass Hello {}\n"); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void formatsShellLineAfterFirstLineAsOrdinaryComment() throws FormatterException { |
| 109 | + // The shell runs the first line of the file only. |
| 110 | + String script = "/* License */\n///usr/bin/env jbang \"$0\" \"$@\" ; exit $?\nclass Hello {}\n"; |
| 111 | + assertThat(FORMATTER.formatSource(script)) |
| 112 | + .isEqualTo("/* License */\n/// usr/bin/env jbang \"$0\" \"$@\" ; exit $?\nclass Hello {}\n"); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void formatsFirstLineThatRunsNoCommandAsOrdinaryComment() throws FormatterException { |
| 117 | + // The first word is not a path, so the shell would have nothing to run. |
| 118 | + assertThat(FORMATTER.formatSource("//Hello from JBang\nclass Hello {}\n")) |
| 119 | + .isEqualTo("// Hello from JBang\nclass Hello {}\n"); |
| 120 | + } |
| 121 | +} |
0 commit comments