| Total Complexity | 11 |
| Total Lines | 120 |
| Duplicated Lines | 34.17 % |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | # |
||
| 76 | class Test_30_single_input(Test_20_Base): |
||
| 77 | |||
| 78 | def test_10(self): |
||
| 79 | a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
||
| 80 | |||
| 81 | infile = os.path.join(self.workdir, "a.json") |
||
| 82 | output = os.path.join(self.workdir, "b.json") |
||
| 83 | |||
| 84 | anyconfig.api.dump(a, infile) |
||
| 85 | self.assertTrue(os.path.exists(infile)) |
||
| 86 | |||
| 87 | TT.main(["dummy", "--silent", "-o", output, infile]) |
||
| 88 | self.assertTrue(os.path.exists(output)) |
||
| 89 | |||
| 90 | def test_20_wo_input_type(self): |
||
| 91 | self._assert_run_and_exit("a.conf") |
||
| 92 | |||
| 93 | View Code Duplication | def test_30_w_get_option(self): |
|
| 94 | d = dict(name="a", a=dict(b=dict(c=[1, 2], d="C"))) |
||
| 95 | |||
| 96 | infile = os.path.join(self.workdir, "a.json") |
||
| 97 | output = os.path.join(self.workdir, "b.json") |
||
| 98 | |||
| 99 | anyconfig.api.dump(d, infile) |
||
| 100 | self.assertTrue(os.path.exists(infile)) |
||
| 101 | |||
| 102 | TT.main(["dummy", "--silent", "-o", output, "--get", "a.b", infile]) |
||
| 103 | self.assertTrue(os.path.exists(output)) |
||
| 104 | |||
| 105 | x = anyconfig.api.load(output) |
||
| 106 | self.assertEqual(x, d['a']['b']) |
||
| 107 | |||
| 108 | View Code Duplication | def test_32_w_set_option(self): |
|
| 109 | d = dict(name="a", a=dict(b=dict(c=[1, 2], d="C"))) |
||
| 110 | |||
| 111 | infile = os.path.join(self.workdir, "a.json") |
||
| 112 | output = os.path.join(self.workdir, "b.json") |
||
| 113 | |||
| 114 | anyconfig.api.dump(d, infile) |
||
| 115 | self.assertTrue(os.path.exists(infile)) |
||
| 116 | |||
| 117 | TT.main(["dummy", "-q", "-o", output, "--set", "a.b.d=E", infile]) |
||
| 118 | self.assertTrue(os.path.exists(output)) |
||
| 119 | |||
| 120 | ref = d.copy() |
||
| 121 | ref['a']['b']['d'] = 'E' |
||
| 122 | |||
| 123 | x = anyconfig.api.load(output) |
||
| 124 | self.assertEqual(x, ref) |
||
| 125 | |||
| 126 | def test_40_ignore_missing(self): |
||
| 127 | infile = os.path.join(os.curdir, "conf_file_should_not_exist.json") |
||
| 128 | assert not os.path.exists(infile) |
||
| 129 | |||
| 130 | self.assertFalse(TT.main(["dummy", "--silent", "-O", "json", |
||
| 131 | "--ignore-missing", infile])) |
||
| 132 | |||
| 133 | def test_50_w_schema(self): |
||
| 134 | (infile, scmfile) = (CNF_0_PATH, SCM_0_PATH) |
||
| 135 | output = os.path.join(self.workdir, "output.json") |
||
| 136 | self.run_and_check_exit_code(["--schema", scmfile, "--validate", |
||
| 137 | infile], 0) |
||
| 138 | self.run_and_check_exit_code(["--schema", scmfile, "-o", output, |
||
| 139 | infile], 0) |
||
| 140 | |||
| 141 | infile2 = os.path.join(self.workdir, "input.yml") |
||
| 142 | cnf = CNF_0.copy() |
||
| 143 | cnf["a"] = "aaa" # Validation should fail. |
||
| 144 | anyconfig.api.dump(cnf, infile2) |
||
| 145 | self.run_and_check_exit_code(["--schema", scmfile, "--validate", |
||
| 146 | infile2], 1) |
||
| 147 | |||
| 148 | def test_52_wo_schema(self): |
||
| 149 | self.run_and_check_exit_code(["--validate", CNF_0_PATH], 1) |
||
| 150 | |||
| 151 | View Code Duplication | def test_54_gen_schema_and_validate_with_it(self): |
|
| 152 | cnf = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
||
| 153 | infile = os.path.join(self.workdir, "cnf.json") |
||
| 154 | output = os.path.join(self.workdir, "out.yaml") |
||
| 155 | anyconfig.api.dump(cnf, infile) |
||
| 156 | |||
| 157 | self.run_and_check_exit_code(["--gen-schema", "-o", output, infile], 0) |
||
| 158 | self.assertTrue(os.path.exists(output)) |
||
| 159 | self.run_and_check_exit_code(["--schema", output, "--validate", |
||
| 160 | infile], 0) |
||
| 161 | |||
| 162 | def test_60_w_arg_option(self): |
||
| 163 | a = dict(name="a", a=1, b=dict(b=[1, 2], c="C"), d=[1, 2]) |
||
| 164 | |||
| 165 | infile = os.path.join(self.workdir, "a.json") |
||
| 166 | output = os.path.join(self.workdir, "b.json") |
||
| 167 | |||
| 168 | anyconfig.api.dump(a, infile) |
||
| 169 | self.assertTrue(os.path.exists(infile)) |
||
| 170 | |||
| 171 | TT.main(["dummy", "--silent", "-o", output, "-A", |
||
| 172 | "a:10;name:x;d:3,4", infile]) |
||
| 173 | self.assertTrue(os.path.exists(output)) |
||
| 174 | |||
| 175 | x = anyconfig.api.load(output) |
||
| 176 | |||
| 177 | self.assertNotEqual(a["name"], x["name"]) |
||
| 178 | self.assertNotEqual(a["a"], x["a"]) |
||
| 179 | self.assertNotEqual(a["d"], x["d"]) |
||
| 180 | |||
| 181 | self.assertEqual(x["name"], 'x') |
||
| 182 | self.assertEqual(x["a"], 10) |
||
| 183 | self.assertEqual(x["d"], [3, 4]) |
||
| 184 | |||
| 185 | def test_70_no_template(self): |
||
| 186 | a = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) |
||
| 187 | |||
| 188 | infile = os.path.join(self.workdir, "a.json") |
||
| 189 | output = os.path.join(self.workdir, "b.json") |
||
| 190 | |||
| 191 | anyconfig.api.dump(a, infile) |
||
| 192 | self.assertTrue(os.path.exists(infile)) |
||
| 193 | |||
| 194 | TT.main(["dummy", "--silent", "-o", output, infile]) |
||
| 195 | self.assertTrue(os.path.exists(output)) |
||
| 196 | |||
| 296 |