def test_parse_error(self): """Given a yaml file with incorrect syntax, throw a warning""" with self.assertRaises(exc.TowerCLIError): parser.extra_vars_loader_wrapper(["a: b\nincorrect == brackets"]) with self.assertRaises(exc.TowerCLIError): parser.extra_vars_loader_wrapper(["a: b\nincorrect = =brackets"])
def test_read_from_file(self): """Give it some with '@' and test that it reads from the file""" mock_open = mock.mock_open() with mock.patch('tower_cli.utils.parser.open', mock_open, create=True): manager = mock_open.return_value.__enter__.return_value manager.read.return_value = 'foo: bar' parser.extra_vars_loader_wrapper(["@fake_file1.yml"]) parser.extra_vars_loader_wrapper(["@fake_file2.yml", "@fake_file3.yml"]) # Ensure that "open" was triggered in test self.assertIn(mock.call("fake_file1.yml", 'r'), mock_open.mock_calls) self.assertIn(mock.call("fake_file2.yml", 'r'), mock_open.mock_calls) self.assertIn(mock.call("fake_file3.yml", 'r'), mock_open.mock_calls)
def test_unicode_dump(self): """Test that data is dumped without unicode character marking.""" for data in self.COMBINATION_DATA: string_rep = parser.extra_vars_loader_wrapper(data[0]) self.assertEqual(yaml.load(string_rep), data[1]) assert "python/unicode" not in string_rep assert "\\n" not in string_rep
def test_combine_raw_params(self): """Given multiple files which all have raw parameters, make sure they are combined in the '_raw_params' key entry""" a_kv = "foo=bar\na" b_kv = "baz=fam\nb" result = parser.extra_vars_loader_wrapper([a_kv, b_kv]) rdict = yaml.load(result) self.assertEqual(rdict['_raw_params'], "a b")
def test_precedence(self): """Test that last value is the one that overwrites the others""" adict = {"a": 1} ayml = yaml.dump(adict) a2dict = {"a": 2} a2yml = yaml.dump(a2dict) result = parser.extra_vars_loader_wrapper([ayml, a2yml]) rdict = yaml.load(result) self.assertEqual(rdict['a'], 2)
def test_many_combinations(self): """Combine yaml with json with bare values, check that key:value pairs are preserved at the end.""" adict = {"a": 1} bdict = {"b": 2} ayml = yaml.dump(adict) bjson = yaml.dump(bdict, default_flow_style=True) cyml = "5" result = parser.extra_vars_loader_wrapper([ayml, bjson, cyml]) rdict = yaml.load(result) self.assertEqual(rdict['a'], 1) self.assertEqual(rdict['b'], 2)
def test_returns_input_text(self): """Give it only one file, and it should give it right back.""" mock_text = "\n\nfoo: baar\n\n\n" self.assertEqual(mock_text, parser.extra_vars_loader_wrapper([mock_text]))