<commit_before>require "test_helper"

require "minitest/autorun"
require "pony"

class CopyrightCheckHookTest < HookTestCase
  REPOS_DIR = File.expand_path File.join(File.dirname(__FILE__), "repos")
  FAKE_MAILER = File.join(File.dirname(__FILE__), "fake_mailer")
  MAILER_FILE = File.join(File.dirname(__FILE__), "mail_params")
  TEST_HOOK_BODY_1 = <<TEST
#!/usr/bin/env ruby
require "ruby_git_hooks/copyright_check"

RubyGitHooks.register CopyrightCheckHook.new("domain" => "onlive.com",
  "from" => "Copyright Enforcement <<EMAIL>>",
  "via" => :sendmail,
  "via_options" => {
    :location => #{FAKE_MAILER.inspect},
    :arguments => '#{MAILER_FILE.inspect}'
  }
)

RubyGitHooks.run
TEST

  def setup
    # Empty out the test repos dir
    Hook.shell! "rm -rf #{File.join(REPOS_DIR, "*")}"

    # Remove test mail file
    Hook.shell! "rm -f #{MAILER_FILE}"

    # Create local parent and child repos with a single shared commit
    Dir.chdir REPOS_DIR

    new_bare_repo
    clone_repo
    new_commit "child_repo", "README"
    git_push
  end

  def test_copyright_post_commit
    add_hook("child_repo", "post-commit", TEST_HOOK_BODY_1)

    new_commit("child_repo", "myfile.rb", <<FILE_CONTENTS)
# No copyright notice!
FILE_CONTENTS

    mail_out = File.read MAILER_FILE

    # Should get email with the most recent commit about
    # myfile.rb, which has no copyright notice.
    assert mail_out.include?(last_commit_sha),
      "Mail message must include latest SHA!"
    assert mail_out.include?("myfile.rb"),
      "Mail message must mention myfile.rb!"
  end

  def test_copyright_no_problem
    add_hook("child_repo", "post-commit", TEST_HOOK_BODY_1)

    new_commit("child_repo", "myfile.rb", <<FILE_CONTENTS)
# Copyright (C) 1941-2013 YoyoDyne, Inc.  All Rights Reserved.
FILE_CONTENTS

    assert !File.exist?(MAILER_FILE), "Copyright test must not send email!"
  end

end<commit_msg>More Copyright Check tests<commit_after>require "test_helper"

require "minitest/autorun"
require "pony"

class CopyrightCheckHookTest < HookTestCase
  REPOS_DIR = File.expand_path File.join(File.dirname(__FILE__), "repos")
  FAKE_MAILER = File.join(File.dirname(__FILE__), "fake_mailer")
  MAILER_FILE = File.join(File.dirname(__FILE__), "mail_params")
  TEST_HOOK_BASIC = <<TEST
#!/usr/bin/env ruby
require "ruby_git_hooks/copyright_check"

RubyGitHooks.register CopyrightCheckHook.new("domain" => "onlive.com",
  "from" => "Copyright Enforcement <<EMAIL>>",
  "via" => :sendmail,
  "via_options" => {
    :location => #{FAKE_MAILER.inspect},
    :arguments => '#{MAILER_FILE.inspect}'
  }
)

RubyGitHooks.run
TEST

  TEST_HOOK_NO_SEND = <<TEST
#!/usr/bin/env ruby
require "ruby_git_hooks/copyright_check"

RubyGitHooks.run CopyrightCheckHook.new("no_send" => true)
TEST

  def setup
    # Empty out the test repos dir
    Hook.shell! "rm -rf #{File.join(REPOS_DIR, "*")}"

    # Remove test mail file
    Hook.shell! "rm -f #{MAILER_FILE}"

    # Create local parent and child repos with a single shared commit
    Dir.chdir REPOS_DIR

    new_bare_repo
    clone_repo
    new_commit "child_repo", "README"
    git_push
  end

  def test_copyright_post_commit
    add_hook("child_repo", "post-commit", TEST_HOOK_BASIC)

    new_commit("child_repo", "myfile.rb", <<FILE_CONTENTS)
# No copyright notice!
FILE_CONTENTS

    mail_out = File.read MAILER_FILE

    # Should get email with the most recent commit about
    # myfile.rb, which has no copyright notice.
    assert mail_out.include?(last_commit_sha),
      "Mail message must include latest SHA!"
    assert mail_out.include?("myfile.rb"),
      "Mail message must mention myfile.rb!"
  end

  def test_copyright_no_send_option
    add_hook("child_repo", "post-commit", TEST_HOOK_NO_SEND)

    new_commit("child_repo", "myfile.rb", <<FILE_CONTENTS)
# No copyright notice!
FILE_CONTENTS

    assert !File.exist?(MAILER_FILE),
      "Copyright test must not send email if 'no_send' is set!"
  end

  def test_copyright_basic_correct
    add_hook("child_repo", "post-commit", TEST_HOOK_BASIC)

    new_commit("child_repo", "myfile.rb", <<FILE_CONTENTS)
# Copyright (C) 1941-2013 YoyoDyne, Inc.  All Rights Reserved.
FILE_CONTENTS

    assert !File.exist?(MAILER_FILE), "Copyright test must not send email!"
  end

  def test_copyright_no_first_year
    add_hook("child_repo", "post-commit", TEST_HOOK_BASIC)

    new_commit("child_repo", "myfile.rb", <<FILE_CONTENTS)
# Copyright (C) 2013 YoyoDyne, Inc.  All Rights Reserved.
FILE_CONTENTS

    assert !File.exist?(MAILER_FILE), "Copyright test must not send email!"
  end

end
{'fraction_non_alphanumeric': 0.1162993672267074, 'fraction_numerical': 0.005018546803403884, 'mean_word_length': 4.380281690140845, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2734727', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1574, 'n_words': 371}