Skip to content

Commit

Permalink
Merge pull request #81 from alechenninger/79-sort-output-yaml-isolate…
Browse files Browse the repository at this point in the history
…-never

Fixes #79: Sort output yaml when isolate is 'never'
  • Loading branch information
alechenninger authored Jan 25, 2017
2 parents 6c3a551 + 5426a5f commit 114a676
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ static class NeverIsolateUpdates implements UpdateStrategy {
@Override
public String getUpdate(YamlSourceData data, Map<String, Object> update) {
return BEGIN_MONARCH_MANAGED + '\n' +
(update.isEmpty() ? "" : yaml.dump(update).trim() + '\n') +
(update.isEmpty() ? "" : yaml.dump(new TreeMap<>(update)).trim() + '\n') +
END_MONARCH_MANAGED;
}
}
Expand Down
55 changes: 55 additions & 0 deletions bin/test/io/github/alechenninger/monarch/YamlDataFormatTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,60 @@ class YamlDataFormatTest {
assert 1 == updated.count('unmanaged')
assert ['unmanaged': 456] == yaml.load(updated)
}

@Test
void shouldSortOutput() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
parser.newSourceData().writeUpdate([
'xyz': 1,
'abc': 2,
'a': 3,
'fgh': 4,
'cde': 5,
'lmnop': 6
], out)

assert new String(out.toByteArray()).contains('''a: 3
abc: 2
cde: 5
fgh: 4
lmnop: 6
xyz: 1''')
}
}

static class WithIsolateAlways {
def fs = Jimfs.newFileSystem()
def parser = new YamlDataFormat(new YamlConfiguration() {
YamlConfiguration.Isolate updateIsolation() { YamlConfiguration.Isolate.ALWAYS }
})

@Test(expected = MonarchException.class)
void shouldNotAllowUpdatesToUnmanagedKeys() {
def sourcePath = fs.getPath('/source.yaml')
Files.write(sourcePath, 'unmanaged: 123'.bytes)
parser.parseData(Files.newInputStream(sourcePath))
.writeUpdate(['unmanaged': 456], Files.newOutputStream(sourcePath))
}

@Test
void shouldSortOutput() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
parser.newSourceData().writeUpdate([
'xyz': 1,
'abc': 2,
'a': 3,
'fgh': 4,
'cde': 5,
'lmnop': 6
], out)

assert new String(out.toByteArray()).contains('''a: 3
abc: 2
cde: 5
fgh: 4
lmnop: 6
xyz: 1''')
}
}
}

0 comments on commit 114a676

Please sign in to comment.