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

Fix #3290: Add support for generic regex pattern matching #3328

Closed
wants to merge 15 commits into from

Conversation

Sparsh1212
Copy link
Contributor

@Sparsh1212 Sparsh1212 commented Jun 14, 2021

Explanation

Fixes #3290: Add support for generic regex pattern matching

The script traverses different layers of the codebase to check for any prohibited file content and filenaming pattern.
To run the script:
bazel run //scripts:pattern_validation_check -- $(pwd)

Tests are added to check for the prohibited file content and filenaming patterns in the scripts/src/test/testfiles directory.
To run the tests:
bazel test //scripts:test_pattern_validation_check
The tests are designed in a way such that, we analyze the script checks over some dummy testfiles. The testfiles directory has dummy test files present for all the checks under their respective directory. They are further segregated into two directories:

  1. fail
  2. pass

fail directory contains the files, which will trigger failure for the script checks.
pass directory contains the files, which will make the checks pass.

Checklist

  • The PR title starts with "Fix #bugnum: ", followed by a short, clear summary of the changes. (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ...".)
  • The PR explanation includes the words "Fixes #bugnum: ..." (or "Fixes part of #bugnum" if the PR only partially fixes an issue).
  • The PR follows the style guide.
  • The PR does not contain any unnecessary auto-generated code from Android Studio.
  • The PR is made from a branch that's not called "develop".
  • The PR is made from a branch that is up-to-date with "develop".
  • The PR's branch is based on "develop" and not on any other branch.
  • The PR is assigned to an appropriate reviewer in both the Assignees and the Reviewers sections.

@Sparsh1212 Sparsh1212 marked this pull request as draft June 14, 2021 15:35
Copy link
Contributor

@anandwana001 anandwana001 left a comment

Choose a reason for hiding this comment

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

DummFile2 should be something valid that does not contains any support library.
Also, rather than using a dummy file name, we can use something more descriptive name.

Add some more description in the PR description on what are the patterns we are testing here.

Thanks @Sparsh1212


// Error to show if any files match the regex.
string failure_message = 2;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

add eof

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -0,0 +1 @@
import android.support.v7.app
Copy link
Contributor

Choose a reason for hiding this comment

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

Add something valid file

Copy link
Contributor

Choose a reason for hiding this comment

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

Also add eof

Copy link
Contributor Author

@Sparsh1212 Sparsh1212 Jun 16, 2021

Choose a reason for hiding this comment

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

Done regarding eof.
Regarding what content we should add here, I am not sure, as this is just meant to be for the path pattern check.
Adding @BenHenning for additional thoughts.

filename_regex: ".+?.kt"
prohibited_content_regex: "^import .+?support.+?$"
failure_message: "AndroidX should be used instead of the support library"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

add eof

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, we can name something which resemble more on prohibiting the use of support library.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

class RegexPatternValidationCheck {
companion object {
@JvmStatic
fun main(vararg args: String) {
Copy link
Contributor

Choose a reason for hiding this comment

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

name should be more descriptive

Copy link
Contributor

Choose a reason for hiding this comment

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

also, we don't need to use JvmStatic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, here it looks like we need it in order to run the kt_jvm_binary.
I tried removing it, but when running the scripts, it gave me an error, that it wasn't able to recognize the static main method.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you try to change the method name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The method name must be main only. as it is necessary for the kt_jvm_binary to run the script. I did this from the reference PR https://github.com/oppia/oppia-android-assets/pull/7/files
In this it is done in the same way.

fun prohibited_Content_Check_Should_Fail() {
assertEquals(
true,
RegexPatternValidationCheck.checkProhibitedContent(
Copy link
Contributor

Choose a reason for hiding this comment

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

separate

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@Test
fun prohibited_Content_Check_Should_Fail() {
assertEquals(
true,
Copy link
Contributor

Choose a reason for hiding this comment

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

use named param

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

repoPath = repoPath,
searchFiles = searchFiles,
fileNameRegexString = "DummyFile1.kt",
prohibitedContentRegexString = "^import .+?support.+?$",
Copy link
Contributor

Choose a reason for hiding this comment

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

can we get this string from some single utility.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, to fetch it from the textproto itself.

searchFiles = searchFiles,
fileNameRegexString = "DummyFile1.kt",
prohibitedContentRegexString = "^import .+?support.+?$",
errorToShow = "AndroidX should be used instead of the support library"
Copy link
Contributor

Choose a reason for hiding this comment

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

same with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

@Test
fun no_Prohibited_Content_Check_Should_Pass() {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@oppiabot
Copy link

oppiabot bot commented Jun 16, 2021

Hi @Sparsh1212, it looks like some changes were requested on this pull request by @anandwana001. PTAL. Thanks!

@Sparsh1212 Sparsh1212 marked this pull request as ready for review June 16, 2021 09:04
@anandwana001 anandwana001 removed their assignment Jun 16, 2021
@anandwana001
Copy link
Contributor

Removing my assignee as I already reviewed it first time.

@Sparsh1212
Copy link
Contributor Author

@anandwana001 Thanks for the review,
Addressed all your comments. PTAL.

@Sparsh1212
Copy link
Contributor Author

@fsharpasharp Can you please take a pass for the Bazel files.

@anandwana001
Copy link
Contributor

@anandwana001 Thanks for the review,
Addressed all your comments. PTAL.

Assigning you back as there are few comments which I suggested earlier that are not addressed.

@anandwana001 anandwana001 removed their assignment Jun 16, 2021
@fsharpasharp fsharpasharp self-requested a review June 16, 2021 14:10
Copy link
Contributor

@fsharpasharp fsharpasharp left a comment

Choose a reason for hiding this comment

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

BUILD.bazel file looks good to me.

@oppiabot
Copy link

oppiabot bot commented Jun 16, 2021

Unassigning @fsharpasharp since they have already approved the PR.

@Sparsh1212
Copy link
Contributor Author

@anandwana001 Thanks for the review,
Addressed all your comments. PTAL.

Assigning you back as there are few comments which I suggested earlier that are not addressed.

Apologies! I missed some of the nits.
Have updated them now. Please let me know if something is still left.

- uses: actions/checkout@v2

- name: Set up Bazel
uses: abhinavsingh/setup-bazel@v3
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 using actions that are created by individuals? @BenHenning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are currently using this action in the unit_tests.yml workflow:

uses: abhinavsingh/setup-bazel@v3

kt_jvm_library(
name = "pattern_validation_lib",
testonly = True,
srcs = glob(["src/java/org/oppia/android/scripts/*.kt"]),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we avoid using glob? In general its better to explicitly list down the files rather than giving a generic rule like this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

kt_jvm_test(
name = "test_pattern_validation_check",
testonly = True,
srcs = glob(["src/test/org/oppia/android/scripts/RegexPatternValidationCheckTest.kt"]),
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here and below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

proto_library(
name = "filename_pattern_validation_structure_proto",
srcs = ["filename_pattern_validation_checks.proto"],
visibility = ["//visibility:public"],
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 need visibility to be public? Again, we like to avoid using public as much as possible. Is it possible to provide a set of subpackages for visibility here and below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

option java_multiple_files = true;

message FileContentChecks {
repeated FileContentCheck file_content_checks = 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment explaining the field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

option java_package = "org.oppia.android.app.model";
option java_multiple_files = true;

message FileContentChecks {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment explaining the message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

repeated FileContentCheck file_content_checks = 1;
}

message FileContentCheck {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment explaining the message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

option java_package = "org.oppia.android.app.model";
option java_multiple_files = true;

message FilenameChecks {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add comments here and below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@vinitamurthi vinitamurthi removed their assignment Jun 17, 2021
@Sparsh1212
Copy link
Contributor Author

@vinitamurthi Addressed all your suggestions. Please take a look.

@Sparsh1212
Copy link
Contributor Author

Sparsh1212 commented Jun 18, 2021

@anandwana001 @vinitamurthi @fsharpasharp @BenHenning
Apologies! had to close this one, as this was from a fork, so the upcoming stacked PRs cannot be chained on this one.
All the review comments have been incorporated in the new PR #3340

@Sparsh1212 Sparsh1212 closed this Jun 18, 2021
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.

Add support for generic regex pattern matching against filename patterns and file contents.
5 participants