2017-04-12 10:52:52 -04: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 11:19:47 -04: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-04-12 10:52:52 -04:00
|
|
|
`cd #{repo_dir} && mkdir desktop mobile common`
|
|
|
|
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
|
|
|
|
|
2017-04-17 15:56:13 -04:00
|
|
|
def about_json(options = {})
|
|
|
|
options[:love] ||= "FAFAFA"
|
|
|
|
|
|
|
|
<<JSON
|
|
|
|
{
|
2017-04-12 10:52:52 -04:00
|
|
|
"name": "awesome theme",
|
|
|
|
"about_url": "https://www.site.com/about",
|
2017-04-17 15:56:13 -04:00
|
|
|
"license_url": "https://www.site.com/license",
|
|
|
|
"color_schemes": {
|
|
|
|
"Amazing": {
|
|
|
|
"love": "#{options[:love]}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JSON
|
|
|
|
end
|
|
|
|
|
|
|
|
let :initial_repo do
|
|
|
|
setup_git_repo(
|
|
|
|
"about.json" => about_json,
|
2017-04-12 10:52:52 -04:00
|
|
|
"desktop/desktop.scss" => "body {color: red;}",
|
|
|
|
"common/header.html" => "I AM HEADER",
|
|
|
|
"common/random.html" => "I AM SILLY",
|
2017-04-12 11:30:16 -04:00
|
|
|
"common/embedded.scss" => "EMBED",
|
2017-04-12 10:52:52 -04: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")
|
|
|
|
|
2017-04-12 11:30:16 -04:00
|
|
|
expect(@theme.theme_fields.length).to eq(3)
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
mapped = Hash[*@theme.theme_fields.map{|f| ["#{f.target}-#{f.name}", f.value]}.flatten]
|
|
|
|
|
|
|
|
expect(mapped["0-header"]).to eq("I AM HEADER")
|
|
|
|
expect(mapped["1-scss"]).to eq("body {color: red;}")
|
2017-04-12 11:30:16 -04:00
|
|
|
expect(mapped["0-embedded_scss"]).to eq("EMBED")
|
2017-04-12 10:52:52 -04:00
|
|
|
|
|
|
|
expect(remote.remote_updated_at).to eq(time)
|
|
|
|
|
2017-04-17 15:56:13 -04: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 10:52:52 -04:00
|
|
|
File.write("#{initial_repo}/common/header.html", "I AM UPDATED")
|
2017-04-17 15:56:13 -04:00
|
|
|
File.write("#{initial_repo}/about.json", about_json(love: "EAEAEA"))
|
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
`cd #{initial_repo} && git commit -am "update"`
|
|
|
|
|
2017-04-17 15:56:13 -04:00
|
|
|
|
2017-04-12 10:52:52 -04:00
|
|
|
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-17 15:56:13 -04: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-04-12 10:52:52 -04:00
|
|
|
mapped = Hash[*@theme.theme_fields.map{|f| ["#{f.target}-#{f.name}", f.value]}.flatten]
|
|
|
|
|
|
|
|
expect(mapped["0-header"]).to eq("I AM UPDATED")
|
|
|
|
expect(mapped["1-scss"]).to eq("body {color: red;}")
|
|
|
|
expect(remote.remote_updated_at).to eq(time)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|