Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ Required Input Variables
Optional Input Variables

- `AC_REPOSITORY_DIR`: Cloned git repository path. Included and excluded paths are defined relative to cloned repository, except `~` prefixed paths.

## Running tests

Requires the [RSpec](https://rspec.info) gem and the Ruby standard library (Coverage, Open3, Digest). No Gemfile or Bundler needed.

```bash
gem install rspec # once
ruby test/test_main.rb
```

The suite covers every function in `main.rb` in-process and runs the full script in a subprocess with the `unzip`/`curl` toolchain and the signed-URL HTTP call stubbed, so no real command is executed and no network is used. A pass/fail summary and a coverage report are printed at the end of each run.
112 changes: 57 additions & 55 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,78 +29,80 @@ def abort_with0(message)
exit 0
end

ac_repository_path = get_env_variable('AC_REPOSITORY_DIR')
ac_cache_label = get_env_variable('AC_CACHE_LABEL') || abort_with0('Cache label path must be defined.')
ac_cache_label = ac_cache_label.gsub(%r{[^A-Za-z0-9_/-]}, '_')
if __FILE__ == $PROGRAM_NAME
ac_repository_path = get_env_variable('AC_REPOSITORY_DIR')
ac_cache_label = get_env_variable('AC_CACHE_LABEL') || abort_with0('Cache label path must be defined.')
ac_cache_label = ac_cache_label.gsub(%r{[^A-Za-z0-9_/-]}, '_')

ac_token_id = get_env_variable('AC_TOKEN_ID') || abort_with0('AC_TOKEN_ID env variable must be set when build started.')
ac_callback_url = get_env_variable('AC_CALLBACK_URL') ||
abort_with0('AC_CALLBACK_URL env variable must be set when build started.')
ac_token_id = get_env_variable('AC_TOKEN_ID') || abort_with0('AC_TOKEN_ID env variable must be set when build started.')
ac_callback_url = get_env_variable('AC_CALLBACK_URL') ||
abort_with0('AC_CALLBACK_URL env variable must be set when build started.')

signed_url_api = "#{ac_callback_url}?action=getCacheUrls"
signed_url_api = "#{ac_callback_url}?action=getCacheUrls"

# check dependencies
run_command('unzip -v |head -1')
run_command('curl --version |head -1')
# check dependencies
run_command('unzip -v |head -1')
run_command('curl --version |head -1')

cache = "ac_cache/#{ac_cache_label}"
zipped = "ac_cache/#{ac_cache_label.gsub('/', '_')}.zip"
cache = "ac_cache/#{ac_cache_label}"
zipped = "ac_cache/#{ac_cache_label.gsub('/', '_')}.zip"

puts '--- Inputs:'
puts ac_cache_label
puts ac_repository_path
puts '-----------'
puts '--- Inputs:'
puts ac_cache_label
puts ac_repository_path
puts '-----------'

env_dirs = Hash.new('')
ENV.each_pair do |k, v|
next unless k.start_with?('AC_')
next if v.include?('//') || v.include?(':')
env_dirs = Hash.new('')
ENV.each_pair do |k, v|
next unless k.start_with?('AC_')
next if v.include?('//') || v.include?(':')

env_dirs[k] = v if File.directory?(v) || %r{^(.+)/([^/]+)$} =~ v
end
env_dirs[k] = v if File.directory?(v) || %r{^(.+)/([^/]+)$} =~ v
end

system("rm -rf #{cache}")
system("mkdir -p #{cache}")
system("rm -rf #{cache}")
system("mkdir -p #{cache}")

unless ac_token_id.empty?
puts ''
unless ac_token_id.empty?
puts ''

ws_signed_url = "#{signed_url_api}&cacheKey=#{ac_cache_label.gsub('/', '_')}&tokenId=#{ac_token_id}"
puts ws_signed_url
ws_signed_url = "#{signed_url_api}&cacheKey=#{ac_cache_label.gsub('/', '_')}&tokenId=#{ac_token_id}"
puts ws_signed_url

uri = URI(ws_signed_url)
response = Net::HTTP.get(uri)
unless response.empty?
puts 'Downloading cache...'
uri = URI(ws_signed_url)
response = Net::HTTP.get(uri)
unless response.empty?
puts 'Downloading cache...'

signed = JSON.parse(response)
ENV['AC_CACHE_GET_URL'] = signed['getUrl']
puts ENV['AC_CACHE_GET_URL']
if get_env_variable('AC_CACHE_PROVIDER').eql?('FILESYSTEM')
run_command_with_log("curl -X GET --fail -o #{zipped} '#{ENV['AC_CACHE_GET_URL']}'")
else
run_command_with_log("curl -X GET -H \"Content-Type: application/zip\" --fail -o #{zipped} $AC_CACHE_GET_URL")
signed = JSON.parse(response)
ENV['AC_CACHE_GET_URL'] = signed['getUrl']
puts ENV['AC_CACHE_GET_URL']
if get_env_variable('AC_CACHE_PROVIDER').eql?('FILESYSTEM')
run_command_with_log("curl -X GET --fail -o #{zipped} '#{ENV['AC_CACHE_GET_URL']}'")
else
run_command_with_log("curl -X GET -H \"Content-Type: application/zip\" --fail -o #{zipped} $AC_CACHE_GET_URL")
end
end
end
end

exit 0 unless File.size?(zipped)
exit 0 unless File.size?(zipped)

md5sum = Digest::MD5.file(zipped).hexdigest
puts "MD5: #{md5sum}"
File.open("#{zipped}.md5", 'a') do |f|
f.puts md5sum.to_s
end
run_command_with_log("unzip -qq -o #{zipped}")
md5sum = Digest::MD5.file(zipped).hexdigest
puts "MD5: #{md5sum}"
File.open("#{zipped}.md5", 'a') do |f|
f.puts md5sum.to_s
end
run_command_with_log("unzip -qq -o #{zipped}")

Dir.glob("#{cache}/**/*.zip", File::FNM_DOTMATCH).each do |zip_file|
puts zip_file
Dir.glob("#{cache}/**/*.zip", File::FNM_DOTMATCH).each do |zip_file|
puts zip_file

last_slash = zip_file.rindex('/')
base_path = zip_file[cache.length..last_slash - 1]
base_path = env_dirs[base_path[1..-1]] if env_dirs.key?(base_path[1..-1])
last_slash = zip_file.rindex('/')
base_path = zip_file[cache.length..last_slash - 1]
base_path = env_dirs[base_path[1..-1]] if env_dirs.key?(base_path[1..-1])

puts base_path
system("mkdir -p #{base_path}")
run_command_with_log("unzip -qq -u -o #{zip_file} -d #{base_path}/")
puts base_path
system("mkdir -p #{base_path}")
run_command_with_log("unzip -qq -u -o #{zip_file} -d #{base_path}/")
end
end
Loading