2019-05-27 17:38:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
def list_files(base_dir, pattern = "*")
|
|
|
|
Dir[File.join("#{base_dir}", pattern)]
|
|
|
|
end
|
|
|
|
|
|
|
|
def grep_files(files, regex)
|
|
|
|
files.select { |file| grep_file(file, regex) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def grep_file(file, regex)
|
|
|
|
lines = File.open(file).grep(regex)
|
|
|
|
lines.count > 0 ? file : nil
|
|
|
|
end
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe "Coding style" do
|
2019-08-08 10:06:27 -04:00
|
|
|
describe "Post Migrations" do
|
|
|
|
def check_offenses(files, method_name, constant_name)
|
|
|
|
method_name_regex = /#{Regexp.escape(method_name)}/
|
|
|
|
constant_name_regex = /#{Regexp.escape(constant_name)}/
|
|
|
|
offenses = files.reject { |file| is_valid?(file, method_name_regex, constant_name_regex) }
|
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
expect(offenses).to be_empty, <<~TEXT
|
2019-08-08 10:06:27 -04:00
|
|
|
You need to use the constant #{constant_name} when you use
|
|
|
|
#{method_name} in order to help with restoring backups.
|
|
|
|
|
|
|
|
Please take a look at existing migrations to see how to use it correctly.
|
|
|
|
|
|
|
|
Offenses:
|
|
|
|
#{offenses.join("\n")}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
TEXT
|
2019-08-08 10:06:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_valid?(file, method_name_regex, constant_name_regex)
|
|
|
|
contains_method_name = File.open(file).grep(method_name_regex).any?
|
|
|
|
contains_constant_name = File.open(file).grep(constant_name_regex).any?
|
|
|
|
|
|
|
|
contains_method_name ? contains_constant_name : true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures dropped tables and columns are stored in constants" do
|
|
|
|
migration_files = list_files("db/post_migrate", "**/*.rb")
|
|
|
|
|
|
|
|
check_offenses(migration_files, "ColumnDropper.execute_drop", "DROPPED_COLUMNS")
|
|
|
|
check_offenses(migration_files, "TableDropper.execute_drop", "DROPPED_TABLES")
|
|
|
|
end
|
|
|
|
end
|
2023-02-14 06:24:49 -05:00
|
|
|
|
|
|
|
describe "non-colocated component templates" do
|
|
|
|
{
|
2023-02-15 05:20:26 -05:00
|
|
|
"discourse" => "app/assets/javascripts/discourse/app/templates/components",
|
2023-02-14 06:24:49 -05:00
|
|
|
"admin" => "app/assets/javascripts/admin/addon/templates/components",
|
2023-02-15 06:29:22 -05:00
|
|
|
"wizard" => "app/assets/javascripts/wizard/addon/templates/components",
|
2023-02-14 06:24:49 -05:00
|
|
|
"chat/discourse" => "plugins/chat/assets/javascripts/discourse/templates/components",
|
|
|
|
"chat/admin" => "plugins/chat/assets/javascripts/admin/templates/components",
|
|
|
|
}.each_pair do |name, dir|
|
|
|
|
it "do not exist for #{name}" do
|
|
|
|
expect(list_files(dir)).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-27 17:38:17 -04:00
|
|
|
end
|