Skip to content

Commit

Permalink
Merge pull request #351 from okta/ignore-io-when-interpolate
Browse files Browse the repository at this point in the history
Interpolator will ignore IOExceptions
  • Loading branch information
arvindkrishnakumar-okta authored Aug 24, 2023
2 parents ab6278c + 40d3c4b commit 8dce4c8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
6 changes: 6 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
27 changes: 18 additions & 9 deletions common/src/main/java/com/okta/cli/common/service/Interpolator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;

public interface Interpolator {

String interpolate(String text, Map<String, String> context);
Expand All @@ -36,22 +35,32 @@ public interface Interpolator {
default void interpolate(Path path, Map<String, String> context) throws IOException {

// TODO - better way to detect binary files?
String fileContent = null;
String fileContent;
try {
fileContent = Files.readString(path, StandardCharsets.UTF_8);
fileContent = readFile(path);
} catch (MalformedInputException e) {
log.debug("skipping binary file: {}", path.getFileName());
return;
} catch (IOException e) {
log.warn("Failed to read file: {}", path.getFileName());
return;
}
String result = interpolate(fileContent, context);
// save a write to disk if nothing changed
if (fileContent == null || result == null || result.equals(fileContent)) {
return;
}
try (OutputStreamWriter writer =
new OutputStreamWriter(new FileOutputStream(path.toFile()), StandardCharsets.UTF_8)) {
writer.write(result);
}

writeFile(path, result);
}

// allows for testing
default String readFile(Path path) throws IOException {
return Files.readString(path, UTF_8);
}

// allows for testing
default void writeFile(Path path, String content) throws IOException {
Files.write(path, content.getBytes(UTF_8));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2022-Present Okta, Inc.
*
* 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 com.okta.cli.common.service

import com.google.common.jimfs.Jimfs
import com.google.common.jimfs.Configuration
import org.testng.annotations.Test

import java.nio.file.FileSystem
import java.nio.file.Files
import java.nio.file.Path

import static java.nio.charset.StandardCharsets.UTF_8
import static java.nio.file.Files.write
import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.Matchers.equalTo
import static org.mockito.Mockito.spy
import static org.mockito.Mockito.when

class DefaultInterpolatorTest {

private final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix())

@Test
void basicInterpolation() {
Path path = createTestFile()
new DefaultInterpolator().interpolate(path, ["foo": "bar"])

String result = Files.readString(path, UTF_8)
assertThat(result, equalTo("file contents bar"))
}

@Test
void fileOpenException() {
Path path = createTestFile()
Interpolator interpolator = spy(new DefaultInterpolator())
when(interpolator.readFile(path)).thenThrow(new IOException("Test exception"))

// does not throw an exception
interpolator.interpolate(path, ["foo": "bar"])
}

Path path(String filename) {
return fileSystem.getPath(filename)
}

Path createTestFile() {
Path path = path("/foo")
write(path, 'file contents ${foo}'.getBytes(UTF_8))
return path
}
}

0 comments on commit 8dce4c8

Please sign in to comment.