Skip to content

Commit

Permalink
Merge c7ce88b into b8aae78
Browse files Browse the repository at this point in the history
  • Loading branch information
JinTotonic committed May 17, 2018
2 parents b8aae78 + c7ce88b commit 87cb9b7
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 45 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/venv/
/.idea
yaml-tools.iml
yaml-tools.iml
*.pyc
.coverage
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Yaml-tools

A set of CLI tools to manipulate YAML files (merge, delete, etc...) with comment preservation
A set of CLI tools to manipulate YAML files (merge, delete, etc...) with comment preservation, based on [ruamel.yaml](http://yaml.readthedocs.io/en/latest/)

## Getting Started

Expand All @@ -21,9 +21,10 @@ A set of CLI tools to manipulate YAML files (merge, delete, etc...) with comment
```
$ python yaml_tools.py <command> [<args>]
```
At the moment there is only one command available :

#### merge
There are only 2 commands at the moments :

### merge
Merge two or more yaml files and preserve the comments
```
$ python yaml_tools.py merge -i INPUTS [INPUTS ...] [-o OUTPUT] [--indent INDENT]
Expand All @@ -32,6 +33,16 @@ $ python yaml_tools.py merge -i INPUTS [INPUTS ...] [-o OUTPUT] [--indent INDENT
- OUTPUT: path to output yaml file (or sys.stdout by default).
- INDENT: number of space(s) for each indent.

### delete
Delete one item from the input yaml file
```
$ python yaml_tools.py delete ITEM_PATH -i INPUT [-o OUTPUT] [--indent INDENT]
```
- ITEM_PATH: yaml item to be deleted, e.g. `key1.list[0].key2`
- INPUT: path to input yaml file.
- OUTPUT: path to output yaml file (or sys.stdout by default).
- INDENT: number of space(s) for each indent.

## Running the tests
```
$ cd src/tests/
Expand Down
9 changes: 9 additions & 0 deletions src/tests/delete/expected_out.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#comment1
test:
foo:
h:
- check: ok
ef: fefe
- ef: fefsegsegs
i: random
bar: 1
11 changes: 11 additions & 0 deletions src/tests/delete/file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#comment1
test:
foo:
h:
- check: ok
ef: fefe
- check: not_ok
ef: fefsegsegs
- fqffqzfq
i: random
bar: 1
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
94 changes: 69 additions & 25 deletions src/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
import unittest
import ruamel.yaml
from ruamel.yaml import YAML
from ruamel.yaml.compat import StringIO

import sys
sys.path.append('..')
import yaml_tools


def _pass(s):
pass
class MyYAML(YAML):
def dump(self, data, stream=None, **kw):
inefficient = False
if stream is None:
inefficient = True
stream = StringIO()
YAML.dump(self, data, stream, **kw)
if inefficient:
return stream.getvalue()


class TestCommands(unittest.TestCase):
merge_str_out = """
#comment1
test:
foo: 2 #comment1
bar: 3 #comment3
foobar: 3 #comment3
"""

def test_unrecognized_command(self):
sys.argv = ['yaml-tools', 'super-unrecognized-command-wtf']
with self.assertRaises(SystemExit) as cm:
yaml_tools.main()
self.assertEqual(cm.exception.code, 1)

def test_fail_delete_command(self):
fi = './delete/file.yml'

class TestMerge(unittest.TestCase):
str_out = """
#comment1
test:
foo: 2 #comment1
bar: 3 #comment3
foobar: 3 #comment3
"""
sys.argv = ['yaml-tools', 'delete', 'unknownKey0', '-i', fi]
self.assertRaises(KeyError, yaml_tools.main)

sys.argv = ['yaml-tools', 'delete', 'unknownKey1.foo', '-i', fi]
self.assertRaises(KeyError, yaml_tools.main)

sys.argv = ['yaml-tools', 'delete', 'test.foo[0].check', '-i', fi]
self.assertRaises(TypeError, yaml_tools.main)

sys.argv = ['yaml-tools', 'delete', 'test.foo.h[10].check', '-i', fi]
self.assertRaises(IndexError, yaml_tools.main)

sys.argv = ['yaml-tools', 'delete', 'test.foo.h[1000]', '-i', fi]
self.assertRaises(IndexError, yaml_tools.main)

sys.argv = ['yaml-tools', 'delete', 'test.foo.unknownKey2[0]', '-i', fi]
self.assertRaises(KeyError, yaml_tools.main)

sys.argv = ['yaml-tools', 'delete', 'test.foo.h[2].unknownKey3', '-i', fi]
self.assertRaises(TypeError, yaml_tools.main)

def test_3_str_merge_with_comment(self):
str1 = """
Expand All @@ -43,33 +74,46 @@ def test_3_str_merge_with_comment(self):
bar: 3 #comment3
foobar: 3 #comment3
"""
out = yaml_tools.successive_merge([str1, str2, str3])
expected_out = ruamel.yaml.round_trip_load(self.str_out)

yml = ruamel.yaml.YAML()
out_str = yml.dump(out, None, transform=_pass)
expected_out_str = yml.dump(expected_out, None, transform=_pass)
out = yaml_tools.successive_merge([str1, str2, str3])
expected_out = ruamel.yaml.round_trip_load(self.merge_str_out)

yml = MyYAML()
out_str = yml.dump(out)
expected_out_str = yml.dump(expected_out)
self.assertEqual(out_str, expected_out_str)

def test_3_files_merge_with_comment(self):
f1 = './files/file1.yml'
f2 = './files/file2.yml'
f3 = './files/file3.yml'
fo = './files/out.yml'
sys.argv = ['yaml-tools', 'merge', '-i', f1, f2, f3, '-o', fo]
f1 = './merge/file1.yml'
f2 = './merge/file2.yml'
f3 = './merge/file3.yml'
fo = './merge/out.yml'
feo = './merge/expected_out.yml'

sys.argv = ['yaml-tools', 'merge', '-i', f1, f2, f3, '-o', fo]
yaml_tools.main()

yml = ruamel.yaml.YAML()
out_file = open(fo, 'r')
out_str = yml.dump(out_file.read(), None, transform=_pass)
expected_out_file = open(feo, 'r')
self.assertEqual(out_file.read(), expected_out_file.read())
out_file.close()
expected_out_file.close()

expected_out = ruamel.yaml.round_trip_load(self.str_out)
expected_out_str = yml.dump(expected_out, None, transform=_pass)
def test_delete_item(self):
fi = './delete/file.yml'
fo = './delete/out.yml'
feo = './delete/expected_out.yml'

self.assertEqual(out_str, expected_out_str)
sys.argv = ['yaml-tools', 'delete', 'test.foo.h[2]', '-i', fi, '-o', fo]
yaml_tools.main()
sys.argv = ['yaml-tools', 'delete', 'test.foo.h[1].check', '-i', fo, '-o', fo]
yaml_tools.main()

out_file = open(fo, 'r')
expected_out_file = open(feo, 'r')
self.assertEqual(out_file.read(), expected_out_file.read())
out_file.close()
expected_out_file.close()


class TestMergeByType(unittest.TestCase):
Expand Down
Loading

0 comments on commit 87cb9b7

Please sign in to comment.