From 2932b6ba76b805d7073f7b72cc395338d57f50a1 Mon Sep 17 00:00:00 2001 From: Maximilian Kaul Date: Tue, 11 Mar 2025 14:14:33 +0100 Subject: [PATCH] Added test EOG after with --- .../aisec/cpg/frontends/python/EOGTest.kt | 58 +++++++++++++++++++ .../test/resources/python/with_stmt_EOG.py | 4 ++ 2 files changed, 62 insertions(+) create mode 100644 cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/EOGTest.kt create mode 100644 cpg-language-python/src/test/resources/python/with_stmt_EOG.py diff --git a/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/EOGTest.kt b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/EOGTest.kt new file mode 100644 index 00000000000..831cafe3c06 --- /dev/null +++ b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/EOGTest.kt @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025, Fraunhofer AISEC. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * $$$$$$\ $$$$$$$\ $$$$$$\ + * $$ __$$\ $$ __$$\ $$ __$$\ + * $$ / \__|$$ | $$ |$$ / \__| + * $$ | $$$$$$$ |$$ |$$$$\ + * $$ | $$ ____/ $$ |\_$$ | + * $$ | $$\ $$ | $$ | $$ | + * \$$$$$ |$$ | \$$$$$ | + * \______/ \__| \______/ + * + */ +package de.fraunhofer.aisec.cpg.frontends.python + +import de.fraunhofer.aisec.cpg.graph.calls +import de.fraunhofer.aisec.cpg.graph.invoke +import de.fraunhofer.aisec.cpg.query.executionPath +import de.fraunhofer.aisec.cpg.test.analyze +import java.nio.file.Path +import kotlin.test.Test +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class EOGTest { + @Test + fun testEOGAfterWithStmt() { + val topLevel = Path.of("src", "test", "resources", "python") + val result = + analyze(listOf(topLevel.resolve("with_stmt_EOG.py").toFile()), topLevel, true) { + it.registerLanguage() + } + assertNotNull(result) + + val read = result.calls("read").singleOrNull() + assertNotNull(read, "Expected to find a `read` call.") + + val print = result.calls("print").singleOrNull() + assertNotNull(print, "Expected to find a `print` call.") + + assertTrue( + executionPath(startNode = read) { it == print }.value, + "Expected to find an execution path from read to print.", + ) + } +} diff --git a/cpg-language-python/src/test/resources/python/with_stmt_EOG.py b/cpg-language-python/src/test/resources/python/with_stmt_EOG.py new file mode 100644 index 00000000000..e1a337e355e --- /dev/null +++ b/cpg-language-python/src/test/resources/python/with_stmt_EOG.py @@ -0,0 +1,4 @@ +with open('foo.txt') as file: + file.read() + +print("Done") \ No newline at end of file