Skip to content

Instantly share code, notes, and snippets.

@snobutaka
Last active August 27, 2017 15:25
Show Gist options
  • Save snobutaka/663ab5c47960daab9a25e004e8e4fe19 to your computer and use it in GitHub Desktop.
Save snobutaka/663ab5c47960daab9a25e004e8e4fe19 to your computer and use it in GitHub Desktop.
Ignore all JUnit tests

README

About

This Ruby script edits Java codes. If there exists JUnit test case with @Test annotation, the script adds @Ignore annotation and import statement for org.junit.Ignore.

How to use

IgnoreAnnotater.new().annotate_ignore_all(YOUR_PROJECT_DIR)

Why such script is needed?

In some project, test cases are not maintained and not executed. There exists many dangerous test cases that spoils local DB records and local file sets. And I cannot know which tests are safe or which tests are dangerous, because there are too many tests.

To fix such project, we need to

  1. Ignore all current test cases.
  2. Setup build setting to execute all tests on every build.
  3. Make sure to add safe tests and fix dangerous tests one by one

This script helps step 1.

#!/usr/bin/env ruby
require 'find'
class IgnoreAnnotater
def annotate_ignore(src_path)
File.open(src_path, "r+") do |file|
# TODO: Add `@Ignore` only if not ignored yet.
content = file.read()
edited = content.gsub!("@Test", "@Test @Ignore")
return unless edited
# Add `import org.junit.Ignore;` if not imported yet.
ignore_import_stmt = "import org.junit.Ignore;"
unless content.include?(ignore_import_stmt)
test_import_stmt = "import org.junit.Test;"
both_import_stmt = ignore_import_stmt + "\n" + test_import_stmt
content.gsub!(test_import_stmt, both_import_stmt)
end
file.seek(0)
file.write(content)
file.truncate(file.pos)
end
puts "Edited #{src_path}"
end
def annotate_ignore_all(src_dir)
Find.find(src_dir) do |path|
next unless java_src?(path)
annotate_ignore(path)
end
end
def java_src?(path)
File.file?(path) && path.end_with?(".java")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment