-
Notifications
You must be signed in to change notification settings - Fork 201
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
Introduce plugin for migrating scalatest #572
base: master
Are you sure you want to change the base?
Conversation
ketkarameya
commented
Aug 11, 2023
- Adding plugin to migrate scalatest
d4f3ce8
to
bf9b4d3
Compare
27bd33a
to
f075ebc
Compare
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 call this scala_test
? Looking at the directory path, I'd expect that to be some testing for Piranha, not a tool to migrate tests. Maybe scala_test_migrator
?
|
||
[tool.poetry.dependencies] | ||
python = "^3.9" | ||
polyglot_piranha = "*" |
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, pytest
below 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.
@@ -0,0 +1,26 @@ | |||
# `scalatest` Migration Plugin | |||
|
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.
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.
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.
plugins/scala_test/main.py
Outdated
from update_imports import update_imports | ||
|
||
def _parse_args(): | ||
parser = argparse.ArgumentParser(description="Migrates scala tests!!!") |
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.
Migrates them to what or from what? Also, longer term, do we need a parameter for a target version which affects which mappings we use?
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.
I have updated the app description, to reflect the version number that we want to update to.
Added a cli-arg with default value .
plugins/scala_test/recipes.py
Outdated
|
||
|
||
|
||
def replace_import_rules_edges( |
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.
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
plugins/scala_test/recipes.py
Outdated
query="((identifier) @x (#eq? @x \"@search_heuristic\"))", | ||
holes={"search_heuristic"}, | ||
) | ||
e1 = OutgoingEdges("find_relevant_files", to=[f"update_import"], scope="File") |
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.
Interesting. This runs every rule in the "update_import" group if "find_relevant_files" matches, right?
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.
exactly. The search heuristic narrows down the scope and prevents us from parsing the entire code base. And then we apply these "update import" rules only within these files
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 |
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.
Shouldn't we have a case where an import like import pkg1.pkg2.{A, B}
is replaced as import pkg1.pkg2.{C, B}
and also the simple import pkg3.pkg4.D
-> import pkg5.pkg6.E
case?
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.
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: a.b.c.D
to a.e.f.g.H
.
Before: import a.b.c.{D, E}
After: import a.{b.c.E, e.f.H}
or add some extra logic to heuristically decide when it is not a good idea to add a nested import, and add a simple import at those times.
From: a.b.c.D
to a.b.c.H
.
Before: import a.b.c.{D, E}
After: import a.b.c.{H, E}
( actually @raviagarwal7 suggested adding simple import and keep the rewrite logic less complicated. We believe that we can use Scala linters to re-org the imports like we want to)
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.
Not sure I understand this. I am not saying the rewrite done in these tests is incorrect, but I think there are missing cases given the rewrite rules you added. Specifically the update_simple_import_{...}
rules. My question here is about test coverage for the rules/logic added.
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): |
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.
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.
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.
I agree. I wanted to that . I will eventually extract out a commons
.
This could moved to commons/test_utilities
I believe that replace_imports
could also be a part of commons/scala
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.
Addressed comments - 43a7628
|
||
[tool.poetry.dependencies] | ||
python = "^3.9" | ||
polyglot_piranha = "*" |
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.
@@ -0,0 +1,26 @@ | |||
# `scalatest` Migration Plugin | |||
|
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.
plugins/scala_test/main.py
Outdated
from update_imports import update_imports | ||
|
||
def _parse_args(): | ||
parser = argparse.ArgumentParser(description="Migrates scala tests!!!") |
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.
I have updated the app description, to reflect the version number that we want to update to.
Added a cli-arg with default value .
plugins/scala_test/recipes.py
Outdated
query="((identifier) @x (#eq? @x \"@search_heuristic\"))", | ||
holes={"search_heuristic"}, | ||
) | ||
e1 = OutgoingEdges("find_relevant_files", to=[f"update_import"], scope="File") |
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.
exactly. The search heuristic narrows down the scope and prevents us from parsing the entire code base. And then we apply these "update import" rules only within these files
plugins/scala_test/recipes.py
Outdated
|
||
|
||
|
||
def replace_import_rules_edges( |
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
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 |
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.
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: a.b.c.D
to a.e.f.g.H
.
Before: import a.b.c.{D, E}
After: import a.{b.c.E, e.f.H}
or add some extra logic to heuristically decide when it is not a good idea to add a nested import, and add a simple import at those times.
From: a.b.c.D
to a.b.c.H
.
Before: import a.b.c.{D, E}
After: import a.b.c.{H, E}
( actually @raviagarwal7 suggested adding simple import and keep the rewrite logic less complicated. We believe that we can use Scala linters to re-org the imports like we want to)
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): |
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.
I agree. I wanted to that . I will eventually extract out a commons
.
This could moved to commons/test_utilities
I believe that replace_imports
could also be a part of commons/scala
|
||
[tool.poetry.dependencies] | ||
python = "^3.9" | ||
polyglot_piranha = "*" |
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, pytest
below 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.
It supports both simple and nested imports. While the simple imports are replaced directly, the nested imports are deleted and the new type is imported (as a simple non-nested import). | ||
Assume that the target type is "a.b.c.d" and the new type is "x.y.z". Then the following rules are generated: | ||
import a.b.c.d -> import x.y.z | ||
import a.b.c.{d, e} -> import x.y.z \n import a.b.c.{d} |
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.
import a.b.c.{d, e} -> import x.y.z \n import a.b.c.{d} | |
import a.b.c.{d, e} -> import x.y.z \n import a.b.c.{e} |
I believe you meant e
here, since d
is the one getting replaced...
Rules LGTM |