Ejemplo n.º 1
0
 def test_snakefile_staggered_parsing(self):
     snakefile = Snakefile(StringIO(self.text))
     token_buffer = list()
     next(snakefile)
     for _ in range(3):
         token_buffer.append(next(snakefile))
     for token in reversed(token_buffer):
         snakefile.denext(token)
     result_sequence = list()
     for _ in range(3):
         result_sequence.append(next(snakefile).string)
     expected_sequence = ["a", ":", "\n"]
     assert expected_sequence == result_sequence
Ejemplo n.º 2
0
    def test_simple_rule_one_input(self):
        stream = StringIO("rule a:\n" f'{TAB * 1}input: "foo.txt"')
        smk = Snakefile(stream)
        formatter = Formatter(smk)

        actual = formatter.get_formatted()
        expected = "rule a:\n" f"{TAB * 1}input:\n" f'{TAB * 2}"foo.txt",\n'

        assert actual == expected
Ejemplo n.º 3
0
 def test_implicitly_unrecognised_keyword(self):
     """
     The keyword lives in the 'base' space, so could also be interpreted as Python
     code.
     In that case black will complain of invalid python and not format it.
     """
     with pytest.raises(InvalidPython):
         stream = StringIO(f"role a: \n" f'{TAB * 1}input: "b"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 4
0
 def test_param_collating(self):
     "The keyword gets collated to the previous parameter value"
     with pytest.raises(InvalidParameterSyntax, match="benchmark"):
         stream = StringIO(
             "rule a: \n"
             "\tcontainer: \n"
             '\t\t"envs/sing.img"\n'
             '\t\t\tbenchmark: "bench.txt"'
         )
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 5
0
 def test_snakefile_sequential_parsing(self):
     istream = StringIO(self.text)
     expected_sequence = istream.read().split()
     istream.seek(0)
     snakefile = Snakefile(istream)
     for expected_word in expected_sequence:
         try:
             parsed_word = next(snakefile).string
             assert expected_word == parsed_word
         except Exception:
             break
Ejemplo n.º 6
0
 def test_keyword_under_indentation(self):
     "The keyword gets interpreted as python code"
     with pytest.raises(InvalidPython, match="benchmark"):
         stream = StringIO(
             "rule a: \n"
             "\tcontainer: \n"
             '\t\t"envs/sing.img"\n'
             'benchmark: "bench.txt"'
             '\toutput: "b"'
         )
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 7
0
 def test_keyword_indented_at_parameter_level(self):
     with pytest.raises(InvalidParameterSyntax, match="output"):
         stream = StringIO(
             (
                 "rule a: \n"
                 "\tinput: \n"
                 '\t\t"f1", \n'
                 "\t\toutput: \n"
                 '\t\t\t"f2"'
             )
         )
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 8
0
    def test_lambda_function_with_multiple_args(self):
        stream = StringIO(
            f"rule a:\n"
            f'{TAB * 1}input: "foo.txt" \n'
            f"{TAB * 1}resources:"
            f"{TAB * 2}mem_mb = lambda wildcards, attempt: attempt * 1000")
        smk = Snakefile(stream)
        formatter = Formatter(smk)

        actual = formatter.get_formatted()
        expected = (
            f"rule a:\n"
            f"{TAB * 1}input:\n"
            f'{TAB * 2}"foo.txt",\n'
            f"{TAB * 1}resources:\n"
            f"{TAB * 2}mem_mb=lambda wildcards, attempt: attempt * 1000,\n")

        assert actual == expected
Ejemplo n.º 9
0
    def test_expand_as_param(self):
        stream = StringIO("rule a:\n"
                          f"{TAB * 1}input: \n"
                          f"{TAB * 2}"
                          'expand("{f}/{p}", f = [1, 2], p = ["1", "2"])\n'
                          f'{TAB * 1}output:"foo.txt","bar.txt"\n')

        smk = Snakefile(stream)
        formatter = Formatter(smk)
        actual = formatter.get_formatted()

        expected = ("rule a:\n"
                    f"{TAB * 1}input:\n"
                    f"{TAB * 2}"
                    'expand("{f}/{p}", f=[1, 2], p=["1", "2"]),\n'
                    f"{TAB * 1}output:\n"
                    f'{TAB * 2}"foo.txt",\n'
                    f'{TAB * 2}"bar.txt",\n')

        assert actual == expected
Ejemplo n.º 10
0
 def test_invalid_name_for_keyword(self):
     with pytest.raises(NamedKeywordError, match="Invalid name.*checkpoint"):
         stream = StringIO("checkpoint (): \n" '\tinput: "a"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 11
0
 def test_key_value_invalid_key_fails(self):
     with pytest.raises(InvalidParameterSyntax, match="Invalid key"):
         stream = StringIO("rule a:" '\n\tinput: \n\t\t2 = "file.txt"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 12
0
 def test_empty_keyword_3(self):
     with pytest.raises(NoParametersError, match="message"):
         stream = StringIO("rule a:" "\n\tthreads: 3" "\n\tmessage:")
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 13
0
 def test_empty_keyword_SMK_NOBREAK(self):
     with pytest.raises(EmptyContextError, match="rule"):
         stream = StringIO("rule a:")
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 14
0
 def test_duplicate_rule_fails_SMK_NOBREAK(self):
     with pytest.raises(DuplicateKeyWordError, match="rule a"):
         stream = StringIO("rule a:\n" '\tinput: "a"\n' "rule a:\n" '\tinput:"b"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 15
0
 def test_single_parameter_keyword_disallows_multiple_parameters(self):
     with pytest.raises(TooManyParameters, match="benchmark"):
         stream = StringIO("rule a:" '\n\tbenchmark: "f1.txt", "f2.txt"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 16
0
 def test_explicitly_unrecognised_keyword(self):
     with pytest.raises(SyntaxError, match="Unrecognised keyword"):
         stream = StringIO("rule a:" "\n\talien_keyword: 3")
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 17
0
 def test_single_parameter_keyword_disallows_kwarg(self):
     with pytest.raises(InvalidParameter, match="container .* positional"):
         stream = StringIO("rule a: \n" '\tcontainer: a = "envs/sing.img"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 18
0
 def test_keyword_cannot_be_named(self):
     with pytest.raises(SyntaxError, match="Colon.*expected"):
         stream = StringIO('workdir a: "/to/dir"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 19
0
 def test_no_newline_in_keyword_context_SMK_NOBREAK(self):
     with pytest.raises(SyntaxError, match="Newline expected"):
         stream = StringIO('rule a: input: "input_file"')
         snakefile = Snakefile(stream)
         Formatter(snakefile)
Ejemplo n.º 20
0
 def test_nocolon(self):
     with pytest.raises(SyntaxError, match="Colon.*expected"):
         stream = StringIO("rule a")
         snakefile = Snakefile(stream)
         Formatter(snakefile)