def create_pattern_file(misuse: Misuse, filename: str) -> Pattern:
     patterns_path = join(misuse.path, "patterns")
     path = join(patterns_path, filename)
     directory = dirname(path)
     if not exists(directory):
         makedirs(directory)
     open(path, 'a').close()
     return Pattern(patterns_path, filename)
Beispiel #2
0
    def test_copy(self):
        destination = "copy"
        uut = Pattern(self.temp_dir, self.pattern_file_name)

        uut.copy(join(self.temp_dir, destination))

        assert exists(
            join(self.temp_dir, join(destination, self.pattern_file_name)))
Beispiel #3
0
    def patterns(self) -> Set[Pattern]:
        if not self._PATTERNS:
            pattern_path = join(self.path, "patterns")
            if isdir(pattern_path):
                self._PATTERNS = set([
                    Pattern(pattern_path, y[len(pattern_path) + 1:])
                    for x in os.walk(pattern_path)
                    for y in glob(os.path.join(x[0], '*.java'))
                ])
            else:
                self._PATTERNS = set()

        return self._PATTERNS
    def test_provided_patterns_run(self):
        self.version._MISUSES = [
            create_misuse("-1-",
                          project=self.project,
                          patterns=[Pattern("-base-", "-P1-")])
        ]

        experiment = Experiment(Experiment.PROVIDED_PATTERNS, self.detector,
                                "-findings_path-")

        run = experiment.get_run(self.version)

        assert_is_instance(run, Run)
        assert_equals(1, len(run.executions))
        assert_is_instance(run.executions[0], DetectOnlyExecution)
    def test_publishes_pattern_code_without_trailing_newlines(
            self, read_mock, post_mock, snippets_mock):
        pattern_code = "public class P1 {}"
        read_mock.return_value = pattern_code + "\n\n\n"
        misuse = create_misuse("-m-",
                               project=self.project,
                               patterns=[Pattern("/", "P1.java")])
        create_version("-v-", project=self.project, misuses=[misuse])

        task = PublishMetadataTask("-compiles-path-", "http://test.url")
        task.process_project_misuse(self.project, misuse)
        task.end()

        assert_equals(
            post_mock.call_args[0][1][0]["patterns"][0]["snippet"]["code"],
            pattern_code)
    def test_publishes_pattern_code_without_preamble(self, read_mock,
                                                     post_mock, snippets_mock):
        pattern_preamble = "package foo;\n" \
                       "import Bar;\n" \
                       "\n"
        pattern_code = "public class P1 {\n" \
                       "  void m() { return; }\n" \
                       "}"
        read_mock.return_value = pattern_preamble + pattern_code
        misuse = create_misuse("-m-",
                               project=self.project,
                               patterns=[Pattern("/", "P1.java")])
        create_version("-v-", project=self.project, misuses=[misuse])

        task = PublishMetadataTask("-compiles-path-", "http://test.url")
        task.process_project_misuse(self.project, misuse)
        task.end()

        assert_equals(
            post_mock.call_args[0][1][0]["patterns"][0]["snippet"]["code"],
            pattern_code)
    def test_publishes_pattern_code(self, read_mock, post_mock, snippets_mock):
        pattern_code = "public class P1 {\n" \
            "  void m() { return; }\n" \
            "}"
        read_mock.return_value = pattern_code
        misuse = create_misuse("-m-",
                               project=self.project,
                               patterns=[Pattern("/base/path", "P1.java")])
        create_version("-v-", project=self.project, misuses=[misuse])

        task = PublishMetadataTask("-compiles-path-", "http://test.url")
        task.process_project_misuse(self.project, misuse)
        task.end()

        assert_equals(post_mock.call_args[0][1][0]["patterns"], [{
            "id": "P1",
            "snippet": {
                "code": pattern_code,
                "first_line": 1
            }
        }])
Beispiel #8
0
 def create_pattern(self, filename):
     pattern = Pattern(self.temp_dir, filename)
     create_file(pattern.path)
     return pattern
Beispiel #9
0
    def test_relative_path_without_extension(self):
        pattern = Pattern("/base", "path/pattern.file")

        assert_equals("path/pattern", pattern.relative_path_without_extension)
Beispiel #10
0
    def test_name(self):
        pattern = Pattern("/base", "path/pattern.file")

        assert_equals("pattern", pattern.name)
Beispiel #11
0
    def test_path(self):
        pattern = Pattern("/base", "pattern")

        assert_equals("/base/pattern", pattern.path)
Beispiel #12
0
 def test_hashable(self):
     path = join("a", "b")
     assert_equals(hash(path), hash(Pattern("a", "b")))
Beispiel #13
0
 def test_to_string_is_path(self):
     assert_equals(join("a", "b"), str(Pattern("a", "b")))
Beispiel #14
0
 def test_no_equality_name(self):
     assert Pattern("p", "a") != Pattern("p", "b")
Beispiel #15
0
 def test_no_equality_path(self):
     assert Pattern("a", "p") != Pattern("b", "p")
Beispiel #16
0
 def test_equality(self):
     assert Pattern("p", "a") == Pattern("p", "a")