Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>org.editorconfig</groupId>
<artifactId>editorconfig-maven-plugin</artifactId>
<version>1.0.0-alpha1</version>

<packaging>maven-plugin</packaging>
<properties>
<junit-jupiter.version>5.9.1</junit-jupiter.version>
<assertj-core.version>3.26.0</assertj-core.version>
Expand Down Expand Up @@ -101,6 +101,11 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.15.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected void onInit(
* Checks, whether the content of the file is compliant with the current setting of the {@link #targetOption}
*
* @param content content of the file
* @param section section that is assigned to the current file. The section is expected to be already merged with all others that present
* in .editorconfig files in the tree.
* @param section section that is assigned to the current file. The section is expected to be already merged with
* all others that present in .editorconfig files in the tree.
*
* @return OptionViolations wrapped
*/
Expand All @@ -60,7 +60,7 @@ protected OptionValidationResult checkInternal(

OptionValue<T> optionValue = getValueFromSection(section);

if (optionValue.isUnset() || optionValue.getValue() == null) {
if (optionValue == null || optionValue.isUnset() || optionValue.getValue() == null) {
return OptionValidationResult.skippedValidation(targetOption);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ protected OptionValidationResult checkInternal(
VerifiersExecutionContext executionContext) {
OptionValue<Charset> expectedCharset = getValueFromSection(section);

if (expectedCharset.isUnset() || expectedCharset.getValue() == null) {
if (expectedCharset == null
|| expectedCharset.isUnset()
|| expectedCharset.getValue() == null) {
return OptionValidationResult.skippedValidation(targetOption);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.SoftAssertions;
import org.editorconfig.plugin.maven.model.EndOfLine;
import org.junit.jupiter.api.Test;

Expand All @@ -18,7 +18,7 @@
class BufferedInputStreamTest {

@Test
void test_readLine_LF() throws IOException {
void getEndOfLine_3LinesLfLfLf_Lf() throws IOException {
// given
String firstRow = "first row\n";
String secondRow = "second row\n";
Expand All @@ -30,17 +30,20 @@ void test_readLine_LF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(firstEndOfLine).isEqualTo(EndOfLine.LINE_FEED);
softAssertions.assertThat(secondEndOfLine).isEqualTo(EndOfLine.LINE_FEED);
softAssertions.assertThat(thirdEndOfLine).isEqualTo(EndOfLine.LINE_FEED);
});
}

@Test
void test_readLine_LF_lastLineIsEOFEnded() throws IOException {
void getEndOfLine_3LinesLfLfEof_Eof() throws IOException {
// given
String firstRow = "first row\n";
String secondRow = "second row\n";
Expand All @@ -52,17 +55,20 @@ void test_readLine_LF_lastLineIsEOFEnded() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.EOF);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(firstEndOfLine).isEqualTo(EndOfLine.LINE_FEED);
softAssertions.assertThat(secondEndOfLine).isEqualTo(EndOfLine.LINE_FEED);
softAssertions.assertThat(thirdEndOfLine).isEqualTo(EndOfLine.EOF);
});
}

@Test
void test_readLine_CRLF() throws IOException {
void getEndOfLine_3LinesCrlfCrlfCrlf_Crlf() throws IOException {
// given
String firstRow = "first row\r\n";
String secondRow = "second row\r\n";
Expand All @@ -74,17 +80,22 @@ void test_readLine_CRLF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(firstEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
softAssertions
.assertThat(secondEndOfLine)
.isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
softAssertions.assertThat(thirdEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
});
}

@Test
void test_readLine_CRLF_lastRowEndWithEOF() throws IOException {
void getEndOfLine_3LinesCrlfCrlfEof_Eof() throws IOException {
// given
String firstRow = "first row\r\n";
String secondRow = "second row\r\n";
Expand All @@ -96,17 +107,22 @@ void test_readLine_CRLF_lastRowEndWithEOF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.EOF);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(firstEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
softAssertions
.assertThat(secondEndOfLine)
.isEqualTo(EndOfLine.CARRIAGE_RERUN_LINE_FEED);
softAssertions.assertThat(thirdEndOfLine).isEqualTo(EndOfLine.EOF);
});
}

@Test
void test_readLine_CR() throws IOException {
void getEndOfLine_3LinesCrCrCr_Cr() throws IOException {
// given
String firstRow = "first row\r";
String secondRow = "second row\r";
Expand All @@ -118,17 +134,20 @@ void test_readLine_CR() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(firstEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN);
softAssertions.assertThat(secondEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN);
softAssertions.assertThat(thirdEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN);
});
}

@Test
void test_readLine_CR_lastRowEndWithEOF() throws IOException {
void getEndOfLine_3LinesCrCrEof_Eof() throws IOException {
// given
String firstRow = "first row\r";
String secondRow = "second row\r";
Expand All @@ -140,13 +159,16 @@ void test_readLine_CR_lastRowEndWithEOF() throws IOException {
new BufferedInputStream(testFromAsciiString(source));

// when
ByteArrayLine first = bufferedInputStream.readLine();
ByteArrayLine second = bufferedInputStream.readLine();
ByteArrayLine third = bufferedInputStream.readLine();

Assertions.assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.CARRIAGE_RERUN);
Assertions.assertThat(third.getEndOfLine()).isEqualTo(EndOfLine.EOF);
EndOfLine firstEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine secondEndOfLine = bufferedInputStream.readLine().getEndOfLine();
EndOfLine thirdEndOfLine = bufferedInputStream.readLine().getEndOfLine();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(firstEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN);
softAssertions.assertThat(secondEndOfLine).isEqualTo(EndOfLine.CARRIAGE_RERUN);
softAssertions.assertThat(thirdEndOfLine).isEqualTo(EndOfLine.EOF);
});
}

private static ByteArrayInputStream testFromAsciiString(String asciiString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.Charset;
import java.util.stream.Stream;

import org.assertj.core.api.SoftAssertions;
import org.editorconfig.plugin.maven.model.EndOfLine;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -26,25 +27,29 @@
class ByteArrayLineTest {

@Test
void test_endOfLine() {

// when.
void isTheLastLine_2LinesLfEof_FalseTrue() {
// given
ByteArrayLine first = new ByteArrayLine(
new byte[] {'H', 'e', 'l', 'l', 'o', '\n'}, 5, EndOfLine.LINE_FEED);
ByteArrayLine second =
new ByteArrayLine(new byte[] {'H', 'e', 'l', 'l', 'o'}, 5, EndOfLine.EOF);

// then.
assertThat(first.getEndOfLine()).isEqualTo(EndOfLine.LINE_FEED);
assertThat(first.isTheLastLine()).isFalse();
assertThat(second.getEndOfLine()).isEqualTo(EndOfLine.EOF);
assertThat(second.isTheLastLine()).isTrue();
// when
EndOfLine firstEndOfLine = first.getEndOfLine();
EndOfLine secondEndOfLine = second.getEndOfLine();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(firstEndOfLine).isEqualTo(EndOfLine.LINE_FEED);
softAssertions.assertThat(secondEndOfLine).isEqualTo(EndOfLine.EOF);
softAssertions.assertThat(first.isTheLastLine()).isFalse();
softAssertions.assertThat(second.isTheLastLine()).isTrue();
});
}

@Test
void test_getContent() {

// when.
void getContentWithEol_3LinesLfCrlfEof_Ok() {
// given
ByteArrayLine first = new ByteArrayLine(
new byte[] {'H', 'e', 'l', 'l', 'o', '\n'}, 5, EndOfLine.LINE_FEED);
ByteArrayLine second = new ByteArrayLine(
Expand All @@ -54,20 +59,28 @@ void test_getContent() {
ByteArrayLine third = new ByteArrayLine(
new byte[] {'B', 'B', 'B', '\0', '\0', '\0', '\0'}, 3, EndOfLine.EOF);

// then.
assertThat(first.getContentWithEol())
.hasSize(6)
.containsExactly('H', 'e', 'l', 'l', 'o', '\n');
assertThat(second.getContentWithEol())
.hasSize(6)
.containsExactly('A', 'l', 'e', 'x', '\r', '\n');
assertThat(third.getContentWithEol()).hasSize(4).containsExactly('B', 'B', 'B', '\0');
// when
byte[] firstContent = first.getContentWithEol();
byte[] secondContent = second.getContentWithEol();
byte[] thirdContent = third.getContentWithEol();

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions
.assertThat(firstContent)
.hasSize(6)
.containsExactly('H', 'e', 'l', 'l', 'o', '\n');
softAssertions
.assertThat(secondContent)
.hasSize(6)
.containsExactly('A', 'l', 'e', 'x', '\r', '\n');
softAssertions.assertThat(thirdContent).hasSize(4).containsExactly('B', 'B', 'B', '\0');
});
}

@Test
void test_lengthWithEol() {

// when.
void lengthWithEol_3LinesLfCrlfEof_Ok() {
// given
ByteArrayLine first = new ByteArrayLine(
new byte[] {'H', 'e', 'l', 'l', 'o', '\n'}, 5, EndOfLine.LINE_FEED);
ByteArrayLine second = new ByteArrayLine(
Expand All @@ -77,33 +90,38 @@ void test_lengthWithEol() {
ByteArrayLine third = new ByteArrayLine(
new byte[] {'B', 'B', 'B', '\0', '\0', '\0', '\0'}, 3, EndOfLine.EOF);

// then.
assertThat(first.lengthWithEoL()).isEqualTo(6);
assertThat(second.lengthWithEoL()).isEqualTo(6);
assertThat(third.lengthWithEoL()).isEqualTo(4);
// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(first.lengthWithEoL()).isEqualTo(6);
softAssertions.assertThat(second.lengthWithEoL()).isEqualTo(6);
softAssertions.assertThat(third.lengthWithEoL()).isEqualTo(4);
});
}

@ParameterizedTest
@MethodSource("test_isEmpty")
void test_isEmpty(String string, EndOfLine endOfLine, boolean expectedIsEmpty) {

// when.
boolean actualIsEmpty = new ByteArrayLine(
string.getBytes(US_ASCII),
string.length() - endOfLine.getLengthInBytes(),
endOfLine)
.isEmpty();

// then.
assertThat(actualIsEmpty).isEqualTo(expectedIsEmpty);
void isEmpty_ListOfStrings_Ok(String string, EndOfLine endOfLine, boolean expected) {
// given
ByteArrayLine line = new ByteArrayLine(
string.getBytes(US_ASCII),
string.length() - endOfLine.getLengthInBytes(),
endOfLine);

// when
boolean actual = line.isEmpty();

// then
assertThat(actual).isEqualTo(expected);
}

@ParameterizedTest
@MethodSource("test_getIndent")
void test_getIndent(ByteArrayLine line, Charset charset, int expectedIndent) {
int indent = line.getIndent(2, charset);
void getIndent_ListOfStrings_Ok(ByteArrayLine line, Charset charset, int expected) {
// when
int actual = line.getIndent(2, charset);

assertThat(indent).isEqualTo(expectedIndent);
// then
assertThat(actual).isEqualTo(expected);
}

static Stream<Arguments> test_getIndent() {
Expand Down
Loading