Skip to content

Commit

Permalink
fix(mpconfig): Do not cutoff file extensions for property names in Di…
Browse files Browse the repository at this point in the history
…rConfigSource

As requested by @pdudits, do not cut off file extensions silently.
People on Windows can deal with text files without a file extension,
better stay consistent.

Relates to #5006
  • Loading branch information
poikilotherm committed Jan 27, 2021
1 parent ce30d87 commit 45d7a33
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -418,30 +418,14 @@ public final static String parsePropertyNameFromPath(Path path, Path rootDir) {
String property = "";
if (! path.getParent().equals(rootDir))
property += rootDir.relativize(path.getParent()).toString() + File.separatorChar;
// 2. ignore all file suffixes after last dot
property += removeFileExtension(path.getFileName().toString());
// 2. add the file name (might be used for mangling in the future)
property += path.getFileName();
// 3. replace all path seps with a ".",
property = property.replace(File.separatorChar, '.');
// so "/config/foo/bar/test/one.txt" becomes "foo/bar/test/one.txt" becomes "foo.bar.test.one" property name
return property;
}

/**
* Litte helper to remove the file extension (not present in Java std functionality)
* @param filename A filename containing a dot, marking the start of the file extension
* @return Filename without a suffix (if present)
*/
public final static String removeFileExtension(String filename) {
if (filename == null || ! filename.contains("."))
return filename;
int lastIndex = filename.lastIndexOf('.');
// dot does not belong to file, but parent dir or
// extension is longer than 3 chars (all clear text formats would have 3 chars max)
if (filename.lastIndexOf(File.separatorChar) > lastIndex || lastIndex < filename.length() - 4)
return filename;
return filename.substring(0, lastIndex);
}

/**
* Actually read the data from the file, assuming UTF-8 formatted content, creating properties from it.
* @param path The file to read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class DirConfigSourceTest {

Expand Down Expand Up @@ -179,17 +180,6 @@ public void testParsePropertyNameFromPath() {
examples.put(subpath( "foo", "bar.test", "ex"), "foo.bar.test.ex");
examples.put(subpath( "foo.bar", "test", "ex"), "foo.bar.test.ex");

// we ignore the last file extension if not more than 3 chars.
// this might lead to unexpected behaviour for a user.
// best advice: do not use dots in filename, only in directory names.
examples.put(subpath( "foo", "bar", "test", "ex.txt"), "foo.bar.test.ex");
examples.put(subpath( "foo", "bar", "test", "ex.tar.gz"), "foo.bar.test.ex.tar");
examples.put(subpath( "foo", "bar", "test", "ex.helo"), "foo.bar.test.ex.helo");
examples.put(subpath( "foo.bar", "test.ex"), "foo.bar.test");
examples.put(subpath( "foo", "bar.test.ex"), "foo.bar.test");
examples.put(subpath( "foo.bar.test.ex"), "foo.bar.test");
examples.put(subpath( "foo", "bar", "test.ex"), "foo.bar.test");

// when & then
for (Map.Entry<Path, String> ex : examples.entrySet()) {
//System.out.println(ex.getKey()+" = "+ex.getValue());
Expand Down

0 comments on commit 45d7a33

Please sign in to comment.