Class: Danger::AndroidUnitTestChecker

Inherits:
Plugin
  • Object
show all
Defined in:
lib/dangermattic/plugins/android_unit_test_checker.rb

Overview

This plugin provides methods to check for the presence of unit tests for newly added classes in a pull request.

Examples:

Check missing unit tests using the default parameters:

android_unit_test_checker.check_missing_tests

Check missing unit tests while excluding certain classes, subclasses, and paths:

android_unit_test_checker.check_missing_tests(
  classes_exceptions: [/ViewHolder$/],
  subclasses_exceptions: [/RecyclerView/],
  path_exceptions: ['*.java', 'org/app/ui/**']
)

Check missing unit tests with a custom bypass label:

android_unit_test_checker.check_missing_tests(bypass_label: 'BypassTestCheck')

Check missing unit tests excluding certain classes, subclasses and paths:

android_unit_test_checker.check_missing_tests(classes_exceptions: [/ViewHolder$/], subclasses_exceptions: [/RecyclerView/], path_exceptions: ['*.java', 'org/app/ui/**'])

See Also:

  • Automattic/dangermattic

Constant Summary collapse

ANY_CLASS_DETECTOR =
/class\s+([A-Z]\w+)\s*(.*?)\s*{/m
CLASS_MODIFIER_DETECTOR =
/((?:\s|public|internal|protected|private|final|abstract|static|data|enum|sealed|value|annotation)*)class\s+([A-Z]\w+)\s*(.*?)\s*({|\n\n)/m
CLASS_MODIFIER_EXCEPTIONS =
[
  /\s*data\s*/,
  /\s*private\s*/,
  /\s*enum\s*/,
  /\s*sealed\s*/,
  /\s*value\s*/,
  /\s*annotation\s*/
].freeze
DEFAULT_CLASSES_EXCEPTIONS =
[
  /ViewHolder$/,
  /Module$/,
  /Button$/
].freeze
DEFAULT_SUBCLASSES_EXCEPTIONS =
[
  /(Fragment|Activity)\b/,
  /RecyclerView/,
  /^BroadcastReceiver$/,
  /^ContentProvider$/,
  /Service$/,
  /View$/,
  /ViewGroup$/,
  /Layout$/
].freeze
DEFAULT_UNIT_TESTS_BYPASS_PR_LABEL =
'unit-tests-exemption'

Instance Method Summary collapse

Instance Method Details

#check_missing_tests(classes_exceptions: DEFAULT_CLASSES_EXCEPTIONS, subclasses_exceptions: DEFAULT_SUBCLASSES_EXCEPTIONS, path_exceptions: [], bypass_label: DEFAULT_UNIT_TESTS_BYPASS_PR_LABEL) ⇒ void

This method returns an undefined value.

Check and warns about missing unit tests for a Git diff, with optional classes/subclasses to ignore and an optional PR label to bypass the checks.

check. Defaults to DEFAULT_CLASSES_EXCEPTIONS. the check. Defaults to DEFAULT_SUBCLASSES_EXCEPTIONS.

Parameters:

  • classes_exceptions (Array<String>) (defaults to: DEFAULT_CLASSES_EXCEPTIONS)

    Optional list of regexes matching class names to exclude from the

  • subclasses_exceptions (Array<String>) (defaults to: DEFAULT_SUBCLASSES_EXCEPTIONS)

    Optional list of regexes matching base class names to exclude from

  • path_exceptions (Array<String>) (defaults to: [])

    Optional list of file paths to exclude from the check. Defaults to [].

  • bypass_label (String) (defaults to: DEFAULT_UNIT_TESTS_BYPASS_PR_LABEL)

    Optional label to indicate we can bypass the check. Defaults to DEFAULT_UNIT_TESTS_BYPASS_PR_LABEL.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dangermattic/plugins/android_unit_test_checker.rb', line 72

def check_missing_tests(classes_exceptions: DEFAULT_CLASSES_EXCEPTIONS,
                        subclasses_exceptions: DEFAULT_SUBCLASSES_EXCEPTIONS,
                        path_exceptions: [],
                        bypass_label: DEFAULT_UNIT_TESTS_BYPASS_PR_LABEL)
  list = find_classes_missing_tests(
    git_diff: git.diff,
    classes_exceptions: classes_exceptions,
    subclasses_exceptions: subclasses_exceptions,
    path_exceptions: path_exceptions
  )

  return if list.empty?

  if danger.github.pr_labels.include?(bypass_label)
    list.each do |c|
      warn("Class `#{c.classname}` is missing tests, but `#{bypass_label}` label was set to ignore this.")
    end
  else
    list.each do |c|
      failure("Please add tests for class `#{c.classname}` (or add `#{bypass_label}` label to ignore this).")
    end
  end
end