Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ketkarameya
Copy link
Collaborator

  • Adding plugin to migrate scalatest

@ketkarameya ketkarameya changed the base branch from master to scala-file-scope August 11, 2023 23:06
@ketkarameya ketkarameya changed the base branch from scala-file-scope to master August 14, 2023 01:47
Copy link
Contributor

@lazaroclapp lazaroclapp left a 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 = "*"
Copy link
Contributor

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes * means latest.

Copy link
Contributor

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.

plugins/pyproject.toml Outdated Show resolved Hide resolved
@@ -0,0 +1,26 @@
# `scalatest` Migration Plugin

Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

from update_imports import update_imports

def _parse_args():
parser = argparse.ArgumentParser(description="Migrates scala tests!!!")
Copy link
Contributor

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?

Copy link
Collaborator Author

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 .




def replace_import_rules_edges(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! Credits - CoPilot

query="((identifier) @x (#eq? @x \"@search_heuristic\"))",
holes={"search_heuristic"},
)
e1 = OutgoingEdges("find_relevant_files", to=[f"update_import"], scope="File")
Copy link
Contributor

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?

Copy link
Collaborator Author

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
Copy link
Contributor

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?

Copy link
Collaborator Author

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)

Copy link
Contributor

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):
Copy link
Contributor

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.

Copy link
Collaborator Author

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

Copy link
Collaborator Author

@ketkarameya ketkarameya left a 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 = "*"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes * means latest.

plugins/pyproject.toml Outdated Show resolved Hide resolved
@@ -0,0 +1,26 @@
# `scalatest` Migration Plugin

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

from update_imports import update_imports

def _parse_args():
parser = argparse.ArgumentParser(description="Migrates scala tests!!!")
Copy link
Collaborator Author

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 .

query="((identifier) @x (#eq? @x \"@search_heuristic\"))",
holes={"search_heuristic"},
)
e1 = OutgoingEdges("find_relevant_files", to=[f"update_import"], scope="File")
Copy link
Collaborator Author

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




def replace_import_rules_edges(
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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):
Copy link
Collaborator Author

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 = "*"
Copy link
Contributor

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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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...

@beyeu107
Copy link

Rules LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants