Skip to content

Commit

Permalink
Fixed source formatter problems with variables, closes #104
Browse files Browse the repository at this point in the history
  • Loading branch information
de-jcup committed Oct 12, 2024
1 parent 2fb5887 commit 253e57a
Show file tree
Hide file tree
Showing 10 changed files with 912 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

/**
Expand All @@ -38,7 +39,7 @@ public class SimpleJenkinsDeclartivePipelineSourceFormatter {
private static final String OPEN_BRACKET = String.valueOf(CHAR_OPEN_BRACKET);
private static final String QUOTED_OPEN_BRACKET = Pattern.quote(OPEN_BRACKET);

VariableEscapeSupport variableEscapeSupport = new VariableEscapeSupport();
VariableLineEscapeSupport variableLineEscapeSupport = new VariableLineEscapeSupport(UUID.randomUUID());

private int indent = 3;

Expand All @@ -63,7 +64,7 @@ public String format(String source) {
private void maskVariables(String[] linesArray) {
for (int i = 0; i < linesArray.length; i++) {
String line = linesArray[i];
linesArray[i]=variableEscapeSupport.escape(line);
linesArray[i]=variableLineEscapeSupport.escape(line);
}
}

Expand Down Expand Up @@ -222,7 +223,7 @@ private String convertToString(List<String> list) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < max; i++) {
String line = list.get(i);
sb.append(variableEscapeSupport.unescape(line));
sb.append(variableLineEscapeSupport.unescape(line));
if (i < max - 1) {
sb.append("\n");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2024 Albert Tregnaghi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*
*/
package de.jcup.jenkinseditor;

import java.util.UUID;

public class VariableLineEscapeSupport {

public static final String END_VAR_ESCAPE = "END___VAR_ESCAPE_";
public static final String START_VAR_ESCAPE = "_START_VAR_ESCAPE_";

private final String unescapedPrefix="${";
private final String unescapedPostfix="}";

private final String escapedPrefix;
private final String escapedPostfix;

VariableLineEscapeSupport(UUID uuid) {
escapedPrefix = attach( START_VAR_ESCAPE, uuid);
escapedPostfix = attach(END_VAR_ESCAPE, uuid);
}

private String attach(String string, UUID uuid) {
String attachment = "";
if (uuid != null) {
attachment = uuid.toString();
}
return string + attachment;
}

public String escape(String line) {
int prefixPos = line.indexOf(unescapedPrefix);
if (prefixPos == -1) {
return line;
}
int unescapedPrefixLength = unescapedPrefix.length();
int unescapedPostfixLength = unescapedPostfix.length();

StringBuilder sb = new StringBuilder(line);
while (prefixPos != -1) {
sb.replace(prefixPos, prefixPos + unescapedPrefixLength, escapedPrefix);

int postFixPos = sb.indexOf(unescapedPostfix, prefixPos + unescapedPrefixLength);
if (postFixPos == -1) {
break;
}
sb.replace(postFixPos, postFixPos + unescapedPostfixLength, escapedPostfix);

prefixPos = sb.indexOf(unescapedPrefix);
}

return sb.toString();
}

public String unescape(String line) {
int prefixPos = line.indexOf(escapedPrefix);
if (prefixPos == -1) {
return line;
}
int escapedPrefixLength = escapedPrefix.length();

StringBuilder sb = new StringBuilder(line);
while (prefixPos != -1) {
sb.replace(prefixPos, prefixPos + escapedPrefixLength, unescapedPrefix);

prefixPos = sb.indexOf(escapedPrefix);
}

int escapedPostfixLength = escapedPostfix.length();
int postFixPos = sb.indexOf(escapedPostfix);
while (postFixPos != -1) {
sb.replace(postFixPos, postFixPos + escapedPostfixLength, unescapedPostfix);

postFixPos = sb.indexOf(escapedPostfix);
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,6 @@ public void before() {
toTest = new SimpleJenkinsDeclartivePipelineSourceFormatter();
}

private void assertFormattedAsExpected(String filePartName) {
/* prepare */
Path originPath = FORMATTER_PARENT_FOLDER.resolve(filePartName + ".jenkinsfile");
Path expectedPath = FORMATTER_PARENT_FOLDER.resolve(filePartName + "-expected.jenkinsfile");

String origin = TestFileReader.DEFAULT.readTestFile(originPath);
String expected = TestFileReader.DEFAULT.readTestFile(expectedPath);

/* execute */
String formatted = toTest.format(origin);

/* test */
assertEquals(expected, formatted);
}

@Test
public void format1_file_formatted_as_expected() throws Exception {
assertFormattedAsExpected("format1");
Expand Down Expand Up @@ -79,11 +64,17 @@ public void format5_file_formatted_as_expected() throws Exception {
public void format6_file_formatted_as_expected() throws Exception {
assertFormattedAsExpected("format6");
}

@Test
public void format7_indention_5_file_formatted_as_expected() throws Exception {
toTest.setIndent(5);
assertFormattedAsExpected("format7");
}

@Test
public void format8_two_vars() throws Exception {
assertFormattedAsExpected("format8");
}

@Test
public void format10_file_formatted_as_expected() throws Exception {
Expand All @@ -94,6 +85,11 @@ public void format10_file_formatted_as_expected() throws Exception {
public void format11_file_formatted_as_expected() throws Exception {
assertFormattedAsExpected("format11");
}

@Test
public void format12_file_formatted_as_expected() throws Exception {
assertFormattedAsExpected("format12");
}

@Test
public void null_works_as_well() {
Expand All @@ -115,4 +111,19 @@ public void pipeline_empty_one_liner() {
assertEquals("pipeline {\n}", toTest.format("pipeline {}"));
}

private void assertFormattedAsExpected(String filePartName) {
/* prepare */
Path originPath = FORMATTER_PARENT_FOLDER.resolve(filePartName + ".jenkinsfile");
Path expectedPath = FORMATTER_PARENT_FOLDER.resolve(filePartName + "-expected.jenkinsfile");

String origin = TestFileReader.DEFAULT.readTestFile(originPath);
String expected = TestFileReader.DEFAULT.readTestFile(expectedPath);

/* execute */
String formatted = toTest.format(origin);

/* test */
assertEquals(expected, formatted);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2024 Albert Tregnaghi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*
*/
package de.jcup.jenkinseditor;

import static org.junit.Assert.*;

import java.util.UUID;

import org.junit.Before;
import org.junit.Test;

public class VariableLineEscapeSupportTest {

private VariableLineEscapeSupport supportToTest;
private String expectedEscapedPrefix;
private String expectedEscapedPostfix;

@Before
public void before() {

UUID uuid = UUID.randomUUID();
expectedEscapedPrefix = VariableLineEscapeSupport.START_VAR_ESCAPE + uuid.toString();
expectedEscapedPostfix = VariableLineEscapeSupport.END_VAR_ESCAPE + uuid.toString();

supportToTest = new VariableLineEscapeSupport(uuid);
}

@Test
public void escape_hello_world() {
assertEquals("hello world", supportToTest.escape("hello world"));
}

@Test
public void unescape_hello_world() {
assertEquals("hello world", supportToTest.unescape("hello world"));
}

@Test
public void escape_hello_var1() {
assertEquals("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix, supportToTest.escape("hello ${env.var1}"));
}

@Test
public void unescape_hello_var1() {
assertEquals("hello ${env.var1}", supportToTest.unescape("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix));
}

@Test
public void escape_hello_var1_and_var2() {
assertEquals("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix,
supportToTest.escape("hello ${env.var1} and ${env.var2}"));
}

@Test
public void unescape_hello_var1_and_var2() {
assertEquals("hello ${env.var1} and ${env.var2}",
supportToTest.unescape("hello " + expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix));
}

@Test
public void escape_var1_and_var2() {
assertEquals(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix,
supportToTest.escape("${env.var1} and ${env.var2}"));
}

@Test
public void escape_var1_var2() {
assertEquals(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + "" + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix, supportToTest.escape("${env.var1}${env.var2}"));
}

@Test
public void escape_var1_dollar_var2() {
assertEquals(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + "$" + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix, supportToTest.escape("${env.var1}$${env.var2}"));
}

@Test
public void unescape_var1_and_var2() {
assertEquals("${env.var1} and ${env.var2}",
supportToTest.unescape(expectedEscapedPrefix + "env.var1" + expectedEscapedPostfix + " and " + expectedEscapedPrefix + "env.var2" + expectedEscapedPostfix));
}
}
Loading

0 comments on commit 253e57a

Please sign in to comment.