2017-04-12 22:52:52 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe RemoteTheme do
|
|
|
|
context '#import_remote' do
|
|
|
|
def setup_git_repo(files)
|
|
|
|
dir = Dir.tmpdir
|
|
|
|
repo_dir = "#{dir}/#{SecureRandom.hex}"
|
|
|
|
`mkdir #{repo_dir}`
|
2017-04-12 23:19:47 +08:00
|
|
|
`cd #{repo_dir} && git init . `
|
|
|
|
`cd #{repo_dir} && git config user.email 'someone@cool.com'`
|
|
|
|
`cd #{repo_dir} && git config user.name 'The Cool One'`
|
2017-05-10 05:20:28 +08:00
|
|
|
`cd #{repo_dir} && mkdir desktop mobile common assets`
|
2017-04-12 22:52:52 +08:00
|
|
|
files.each do |name, data|
|
|
|
|
File.write("#{repo_dir}/#{name}", data)
|
|
|
|
`cd #{repo_dir} && git add #{name}`
|
|
|
|
end
|
|
|
|
`cd #{repo_dir} && git commit -am 'first commit'`
|
|
|
|
repo_dir
|
|
|
|
end
|
|
|
|
|
2018-03-15 15:26:54 +08:00
|
|
|
def about_json(love_color: "FAFAFA", color_scheme_name: "Amazing")
|
|
|
|
<<~JSON
|
2017-04-18 03:56:13 +08:00
|
|
|
{
|
2017-04-12 22:52:52 +08:00
|
|
|
"name": "awesome theme",
|
|
|
|
"about_url": "https://www.site.com/about",
|
2017-04-18 03:56:13 +08:00
|
|
|
"license_url": "https://www.site.com/license",
|
2017-05-10 05:20:28 +08:00
|
|
|
"assets": {
|
|
|
|
"font": "assets/awesome.woff2"
|
|
|
|
},
|
|
|
|
"fields": {
|
|
|
|
"color": {
|
|
|
|
"target": "desktop",
|
|
|
|
"value": "#FEF",
|
|
|
|
"type": "color"
|
|
|
|
},
|
|
|
|
"name": "sam"
|
|
|
|
},
|
2017-04-18 03:56:13 +08:00
|
|
|
"color_schemes": {
|
2018-03-15 15:26:54 +08:00
|
|
|
"#{color_scheme_name}": {
|
|
|
|
"love": "#{love_color}"
|
2017-04-18 03:56:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 15:26:54 +08:00
|
|
|
JSON
|
2017-04-18 03:56:13 +08:00
|
|
|
end
|
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
let :scss_data do
|
|
|
|
"@font-face { font-family: magic; src: url($font)}; body {color: $color; content: $name;}"
|
|
|
|
end
|
|
|
|
|
2017-04-18 03:56:13 +08:00
|
|
|
let :initial_repo do
|
|
|
|
setup_git_repo(
|
|
|
|
"about.json" => about_json,
|
2017-05-10 05:20:28 +08:00
|
|
|
"desktop/desktop.scss" => scss_data,
|
2017-04-12 22:52:52 +08:00
|
|
|
"common/header.html" => "I AM HEADER",
|
|
|
|
"common/random.html" => "I AM SILLY",
|
2017-04-12 23:30:16 +08:00
|
|
|
"common/embedded.scss" => "EMBED",
|
2017-05-10 05:20:28 +08:00
|
|
|
"assets/awesome.woff2" => "FAKE FONT",
|
2018-03-05 08:04:23 +08:00
|
|
|
"settings.yaml" => "boolean_setting: true"
|
2017-04-12 22:52:52 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
`rm -fr #{initial_repo}`
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can correctly import a remote theme' do
|
|
|
|
|
|
|
|
time = Time.new('2000')
|
|
|
|
freeze_time time
|
|
|
|
|
|
|
|
@theme = RemoteTheme.import_theme(initial_repo)
|
|
|
|
remote = @theme.remote_theme
|
|
|
|
|
|
|
|
expect(@theme.name).to eq('awesome theme')
|
|
|
|
expect(remote.remote_url).to eq(initial_repo)
|
|
|
|
expect(remote.remote_version).to eq(`cd #{initial_repo} && git rev-parse HEAD`.strip)
|
|
|
|
expect(remote.local_version).to eq(`cd #{initial_repo} && git rev-parse HEAD`.strip)
|
|
|
|
|
|
|
|
expect(remote.about_url).to eq("https://www.site.com/about")
|
|
|
|
expect(remote.license_url).to eq("https://www.site.com/license")
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(@theme.theme_fields.length).to eq(7)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
mapped = Hash[*@theme.theme_fields.map { |f| ["#{f.target_id}-#{f.name}", f.value] }.flatten]
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
expect(mapped["0-header"]).to eq("I AM HEADER")
|
2017-05-10 05:20:28 +08:00
|
|
|
expect(mapped["1-scss"]).to eq(scss_data)
|
2017-04-12 23:30:16 +08:00
|
|
|
expect(mapped["0-embedded_scss"]).to eq("EMBED")
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
expect(mapped["1-color"]).to eq("#FEF")
|
|
|
|
expect(mapped["0-font"]).to eq("")
|
|
|
|
expect(mapped["0-name"]).to eq("sam")
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(mapped["3-yaml"]).to eq("boolean_setting: true")
|
|
|
|
|
|
|
|
expect(mapped.length).to eq(7)
|
|
|
|
|
|
|
|
expect(@theme.settings.length).to eq(1)
|
|
|
|
expect(@theme.settings.first.value).to eq(true)
|
2017-05-10 05:20:28 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
expect(remote.remote_updated_at).to eq(time)
|
|
|
|
|
2017-04-18 03:56:13 +08:00
|
|
|
scheme = ColorScheme.find_by(theme_id: @theme.id)
|
|
|
|
expect(scheme.name).to eq("Amazing")
|
|
|
|
expect(scheme.colors.find_by(name: 'love').hex).to eq('fafafa')
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
File.write("#{initial_repo}/common/header.html", "I AM UPDATED")
|
2018-03-15 15:26:54 +08:00
|
|
|
File.write("#{initial_repo}/about.json", about_json(love_color: "EAEAEA"))
|
2017-04-18 03:56:13 +08:00
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
File.write("#{initial_repo}/settings.yml", "integer_setting: 32")
|
|
|
|
`cd #{initial_repo} && git add settings.yml`
|
|
|
|
|
|
|
|
File.delete("#{initial_repo}/settings.yaml")
|
2017-04-12 22:52:52 +08:00
|
|
|
`cd #{initial_repo} && git commit -am "update"`
|
|
|
|
|
|
|
|
time = Time.new('2001')
|
|
|
|
freeze_time time
|
|
|
|
|
|
|
|
remote.update_remote_version
|
|
|
|
expect(remote.commits_behind).to eq(1)
|
|
|
|
expect(remote.remote_version).to eq(`cd #{initial_repo} && git rev-parse HEAD`.strip)
|
|
|
|
|
|
|
|
remote.update_from_remote
|
|
|
|
@theme.save
|
|
|
|
@theme.reload
|
|
|
|
|
2017-04-18 03:56:13 +08:00
|
|
|
scheme = ColorScheme.find_by(theme_id: @theme.id)
|
|
|
|
expect(scheme.name).to eq("Amazing")
|
|
|
|
expect(scheme.colors.find_by(name: 'love').hex).to eq('eaeaea')
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
mapped = Hash[*@theme.theme_fields.map { |f| ["#{f.target_id}-#{f.name}", f.value] }.flatten]
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
expect(mapped["0-header"]).to eq("I AM UPDATED")
|
2017-05-10 05:20:28 +08:00
|
|
|
expect(mapped["1-scss"]).to eq(scss_data)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
expect(@theme.settings.length).to eq(1)
|
|
|
|
expect(@theme.settings.first.value).to eq(32)
|
|
|
|
|
|
|
|
expect(remote.remote_updated_at).to eq(time)
|
2018-03-15 15:26:54 +08:00
|
|
|
|
|
|
|
# It should be able to remove old colors as well
|
|
|
|
File.write("#{initial_repo}/about.json", about_json(love_color: "BABABA", color_scheme_name: "Amazing 2"))
|
|
|
|
`cd #{initial_repo} && git commit -am "update"`
|
|
|
|
|
|
|
|
remote.update_from_remote
|
|
|
|
@theme.save
|
|
|
|
@theme.reload
|
|
|
|
|
|
|
|
scheme_count = ColorScheme.where(theme_id: @theme.id).count
|
|
|
|
expect(scheme_count).to eq(1)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
2018-08-03 07:53:48 +08:00
|
|
|
|
2018-08-06 13:29:15 +08:00
|
|
|
let(:github_repo) do
|
|
|
|
RemoteTheme.create!(
|
|
|
|
remote_url: "https://github.com/org/testtheme.git",
|
|
|
|
local_version: "a2ec030e551fc8d8579790e1954876fe769fe40a",
|
|
|
|
remote_version: "21122230dbfed804067849393c3332083ddd0c07",
|
|
|
|
commits_behind: 2
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:gitlab_repo) do
|
|
|
|
RemoteTheme.create!(
|
|
|
|
remote_url: "https://gitlab.com/org/repo.git",
|
|
|
|
local_version: "a2ec030e551fc8d8579790e1954876fe769fe40a",
|
|
|
|
remote_version: "21122230dbfed804067849393c3332083ddd0c07",
|
|
|
|
commits_behind: 5
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#github_diff_link" do
|
|
|
|
it "is blank for non-github repos" do
|
|
|
|
expect(gitlab_repo.github_diff_link).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns URL for comparing between local_version and remote_version" do
|
|
|
|
expect(github_repo.github_diff_link).to eq(
|
|
|
|
"https://github.com/org/testtheme/compare/#{github_repo.local_version}...#{github_repo.remote_version}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is blank when theme is up-to-date" do
|
|
|
|
github_repo.update!(local_version: github_repo.remote_version, commits_behind: 0)
|
|
|
|
expect(github_repo.reload.github_diff_link).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-08 21:24:11 +08:00
|
|
|
context ".joined_remotes" do
|
|
|
|
it "finds records that are associated with themes" do
|
|
|
|
github_repo
|
|
|
|
gitlab_repo
|
|
|
|
expect(RemoteTheme.joined_remotes).to eq([])
|
|
|
|
|
|
|
|
Fabricate(:theme, remote_theme: github_repo)
|
|
|
|
expect(RemoteTheme.joined_remotes).to eq([github_repo])
|
|
|
|
|
|
|
|
Fabricate(:theme, remote_theme: gitlab_repo)
|
|
|
|
expect(RemoteTheme.joined_remotes).to contain_exactly(github_repo, gitlab_repo)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-03 07:53:48 +08:00
|
|
|
context ".out_of_date_themes" do
|
|
|
|
let(:remote) { RemoteTheme.create!(remote_url: "https://github.com/org/testtheme") }
|
2018-08-08 12:46:34 +08:00
|
|
|
let!(:theme) { Fabricate(:theme, remote_theme: remote) }
|
2018-08-03 07:53:48 +08:00
|
|
|
|
|
|
|
it "finds out of date themes" do
|
|
|
|
remote.update!(local_version: "old version", remote_version: "new version", commits_behind: 2)
|
|
|
|
expect(described_class.out_of_date_themes).to eq([[theme.name, theme.id]])
|
|
|
|
|
|
|
|
remote.update!(local_version: "new version", commits_behind: 0)
|
|
|
|
expect(described_class.out_of_date_themes).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2018-09-08 21:24:11 +08:00
|
|
|
|
|
|
|
context ".unreachable_themes" do
|
|
|
|
let(:remote) { RemoteTheme.create!(remote_url: "https://github.com/org/testtheme", last_error_text: "can't contact this repo :(") }
|
|
|
|
let!(:theme) { Fabricate(:theme, remote_theme: remote) }
|
|
|
|
|
|
|
|
it "finds out of date themes" do
|
|
|
|
expect(described_class.unreachable_themes).to eq([[theme.name, theme.id]])
|
|
|
|
|
|
|
|
remote.update!(last_error_text: nil)
|
|
|
|
expect(described_class.unreachable_themes).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|