-
Notifications
You must be signed in to change notification settings - Fork 206
Introduce plugin for migrating scalatest #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ target | |
| Cargo.lock | ||
| tmp_test* | ||
| env/ | ||
| **.egg-info | ||
|
|
||
|
|
||
| # Dependencies | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=42", "wheel"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [tool.poetry] | ||
| name = "scala_test" | ||
| version = "0.0.1" | ||
| description = "Rules to migrate 'scaletest'" | ||
| # Add any other metadata you need | ||
|
|
||
| [tool.poetry.dependencies] | ||
| python = "^3.9" | ||
| polyglot_piranha = "*" | ||
|
|
||
| [tool.poetry.dev-dependencies] | ||
| pytest = "*" | ||
|
|
||
| # [tool.poetry.scripts] | ||
| # scala_test = "scala_test.main:main" | ||
ketkarameya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [tool.poetry.scripts."scala_test"] | ||
| main = "scala_test.main:main" | ||
|
|
||
| [tool.poetry.scripts."pytest"] | ||
| main = "pytest" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # `scalatest` Migration Plugin | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs a description/explanation of what this is, before the Usage instructions. Would also be a good point to note if this is a WIP or already functional and for which cases.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| ## Usage: | ||
|
|
||
| Clone the repository - `git clone https://github.com/uber/piranha.git` | ||
|
|
||
| Install the dependencies - `pip3 install -r plugins/scala_test/requirements.txt` | ||
|
|
||
| Run the tool - `python3 plugins/scala_test/main.py -h` | ||
|
|
||
| CLI: | ||
| ``` | ||
| usage: main.py [-h] --path_to_codebase PATH_TO_CODEBASE | ||
|
|
||
| Migrates scala tests!!! | ||
|
|
||
| options: | ||
| -h, --help show this help message and exit | ||
| --path_to_codebase PATH_TO_CODEBASE | ||
| Path to the codebase directory. | ||
| ``` | ||
|
|
||
| ## Test | ||
| ``` | ||
| pytest plugins/scala_test | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import argparse | ||
| from update_imports import update_imports | ||
|
|
||
| def _parse_args(): | ||
| parser = argparse.ArgumentParser(description="Migrates scala tests!!!") | ||
|
||
| parser.add_argument( | ||
| "--path_to_codebase", | ||
| required=True, | ||
| help="Path to the codebase directory.", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
| return args | ||
|
|
||
| def main(): | ||
| args = _parse_args() | ||
| update_imports(args.path_to_codebase, dry_run=True) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,83 @@ | ||||||
| from polyglot_piranha import Rule, OutgoingEdges, RuleGraph, PiranhaArguments, execute_piranha | ||||||
|
|
||||||
| def replace_imports( | ||||||
| target_new_types: dict[str, str], search_heuristic: str, path_to_codebase: str, | ||||||
| dry_run = False | ||||||
| ): | ||||||
| find_relevant_files = Rule( | ||||||
| name="find_relevant_files", | ||||||
| query="((identifier) @x (#eq? @x \"@search_heuristic\"))", | ||||||
| holes={"search_heuristic"}, | ||||||
| ) | ||||||
| e1 = OutgoingEdges("find_relevant_files", to=[f"update_import"], scope="File") | ||||||
|
||||||
|
|
||||||
| rules = [find_relevant_files] | ||||||
| edges = [e1] | ||||||
|
|
||||||
| for target_type, new_type in target_new_types.items(): | ||||||
| rs, es = replace_import_rules_edges(target_type, new_type) | ||||||
| rules.extend(rs) | ||||||
| edges.extend(es) | ||||||
|
|
||||||
| rule_graph = RuleGraph(rules=rules, edges=edges) | ||||||
|
|
||||||
| args= PiranhaArguments( | ||||||
| language="scala", | ||||||
| path_to_codebase=path_to_codebase, | ||||||
| rule_graph=rule_graph, | ||||||
| substitutions={"search_heuristic": f"{search_heuristic}"}, | ||||||
| dry_run=dry_run | ||||||
| ) | ||||||
|
|
||||||
| return execute_piranha(args) | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| def replace_import_rules_edges( | ||||||
|
||||||
| def replace_import_rules_edges( | |
| def replace_import_rules_and_edges( |
Otherwise it reads as generating the "rules' edges" and it might be surprising that it also returns a list of rules.
Btw, these methods and update_imports could use docs. Maybe even those in test_update_imports too, but I leave that up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done! Credits - CoPilot
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| polyglot-piranha | ||
| pytest |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.scala.piranha | ||
|
|
||
| import com.uber.michelangelo.AbstractSparkSuite | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.types.{DoubleType, StringType, StructField, StructType} | ||
| import org.scalatest.{BeforeAndAfter} | ||
| import org.scalatest.matchers.should.Matchers | ||
| import org.scalatestplus.mockito.MockitoSugar |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.scala.piranha | ||
|
|
||
| import com.uber.michelangelo.AbstractSparkSuite | ||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.types.{DoubleType, StringType, StructField, StructType} | ||
| import org.scalatest.{BeforeAndAfter, Matchers} | ||
| import org.scalatest.mock.MockitoSugar | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we have a case where an import like
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm. Actually the solution u suggest looks clean when the before and after type have a significant overlap in their qualified name. Else we have to "infer" the level to split the type name. From:
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from logging import debug, error | ||
| from pathlib import Path | ||
|
|
||
| from os.path import join, basename | ||
| from os import listdir | ||
|
|
||
| from update_imports import update_imports | ||
| # from update_imports import update_imports | ||
|
|
||
| def test_update_imports(): | ||
| summary = update_imports("plugins/scala_test/tests/resources/input/", dry_run=True) | ||
| assert is_as_expected("plugins/scala_test/tests/resources/", summary) | ||
|
|
||
| def is_as_expected(path_to_scenario, output_summary): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wonder if there is a clean way to avoid the duplication between this code and the top level test harness logic. Maybe a shared test utilities library? Not a big deal, but if every plugin will have it's own copy of this code that might be a pain when you need to update something.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I wanted to that . I will eventually extract out a |
||
| expected_output = join(path_to_scenario, "expected") | ||
| print("Summary", output_summary) | ||
| input_dir = join(path_to_scenario, "input") | ||
| for file_name in listdir(expected_output): | ||
| with open(join(expected_output, file_name), "r") as f: | ||
| file_content = f.read() | ||
| expected_content = "".join(file_content.split()) | ||
|
|
||
| # Search for the file in the output summary | ||
| updated_content = [ | ||
| "".join(o.content.split()) | ||
| for o in output_summary | ||
| if basename(o.path) == file_name | ||
| ] | ||
| print(file_name) | ||
| # Check if the file was rewritten | ||
| if updated_content: | ||
| if expected_content != updated_content[0]: | ||
| error("----update" + updated_content[0] ) | ||
| return False | ||
| else: | ||
| # The scenario where the file is not expected to be rewritten | ||
| original_content= Path(join(input_dir, file_name)).read_text() | ||
| if expected_content != "".join(original_content.split()): | ||
| return False | ||
| return True | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from recipes import replace_imports | ||
|
|
||
|
|
||
| IMPORT_MAPPING = { | ||
| "org.scalatest.Matchers": "org.scalatest.matchers.should.Matchers", | ||
| "org.scalatest.mock.MockitoSugar": "org.scalatestplus.mockito.MockitoSugar", | ||
| # Todo write test scenarios for these | ||
| "org.scalatest.FunSuite":"org.scalatest.funsuite.AnyFunSuite", | ||
| "org.scalatest.junit.JUnitRunner":"org.scalatestplus.junit.JUnitRunner", | ||
| "org.scalatest.FlatSpec": "org.scalatest.flatspec.AnyFlatSpec", | ||
| "org.scalatest.junit.AssertionsForJUnit": "org.scalatestplus.junit.AssertionsForJUnit", | ||
| } | ||
|
|
||
| def update_imports(path_to_codebase: str, dry_run = False): | ||
| return replace_imports(IMPORT_MAPPING, "scalatest", path_to_codebase, dry_run) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does
"*"mean here? Any version? Latest?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
*means latest.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to be silently latest? Could we have this just be in sync with the current released Piranha version? (Also,
pytestbelow should probably be set to a concrete library and we should manually keep the dep up to date, no?). Basically, just in terms of reproducibility I am wary of dependencies without a explicit version.