local_pac 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/Gemfile +2 -0
  2. data/bin/local_pac +47 -2
  3. data/features/initializer.feature +23 -0
  4. data/features/show_config.feature +22 -0
  5. data/features/support/env.rb +1 -1
  6. data/files/config.yaml +6 -0
  7. data/files/git-hook.erb +24 -0
  8. data/lib/local_pac/actions/create_directory.rb +33 -0
  9. data/lib/local_pac/actions/create_file.rb +48 -0
  10. data/lib/local_pac/actions/create_repository.rb +38 -0
  11. data/lib/local_pac/config.rb +64 -0
  12. data/lib/local_pac/data.rb +23 -0
  13. data/lib/local_pac/erb_generator.rb +34 -0
  14. data/lib/local_pac/exceptions.rb +9 -0
  15. data/lib/local_pac/file_server.rb +10 -3
  16. data/lib/local_pac/fileable.rb +12 -0
  17. data/lib/local_pac/git.rb +63 -0
  18. data/lib/local_pac/git_file.rb +18 -0
  19. data/lib/local_pac/git_repository.rb +52 -0
  20. data/lib/local_pac/initializer.rb +28 -0
  21. data/lib/local_pac/local_storage.rb +21 -0
  22. data/lib/local_pac/logger.rb +0 -5
  23. data/lib/local_pac/main.rb +24 -0
  24. data/lib/local_pac/pac_file.rb +5 -5
  25. data/lib/local_pac/runner.rb +29 -0
  26. data/lib/local_pac/spec_helper_file_server.rb +41 -0
  27. data/lib/local_pac/template_file.rb +19 -0
  28. data/lib/local_pac/template_repository.rb +22 -0
  29. data/lib/local_pac/ui_logger.rb +12 -0
  30. data/lib/local_pac/version.rb +1 -1
  31. data/lib/local_pac.rb +30 -4
  32. data/local_pac.gemspec +1 -0
  33. data/share/examples/config.yaml +5 -0
  34. data/spec/actions/create_directory_spec.rb +47 -0
  35. data/spec/actions/create_file_spec.rb +95 -0
  36. data/spec/actions/create_repository_spec.rb +47 -0
  37. data/spec/config_spec.rb +113 -0
  38. data/spec/data_spec.rb +34 -0
  39. data/spec/erb_generator_spec.rb +31 -0
  40. data/spec/features/fetch_proxy_pac_spec.rb +30 -12
  41. data/spec/git_repository_spec.rb +60 -0
  42. data/spec/git_spec.rb +100 -0
  43. data/spec/initializer_spec.rb +29 -0
  44. data/spec/local_storage_spec.rb +59 -0
  45. data/spec/pac_file_spec.rb +14 -14
  46. data/spec/runner_spec.rb +42 -0
  47. data/spec/support/git.rb +52 -0
  48. data/spec/support/reporting.rb +1 -0
  49. data/spec/support/rspec.rb +3 -0
  50. data/spec/template_file_spec.rb +25 -0
  51. data/spec/template_repository_spec.rb +44 -0
  52. metadata +74 -5
  53. data/lib/local_pac/pac_manager.rb +0 -55
  54. data/spec/pac_manager_spec.rb +0 -52
@@ -1,19 +1,19 @@
1
1
  # encoding: utf-8
2
2
  module LocalPac
3
3
  class PacFile
4
- attr_reader :path
4
+ attr_reader :file
5
5
  attr_accessor :compressed_content
6
6
 
7
- def initialize(path)
8
- @path = path
7
+ def initialize(file)
8
+ @file = file
9
9
  end
10
10
 
11
11
  def name
12
- File.basename(@path, '.pac').to_sym
12
+ file.name
13
13
  end
14
14
 
15
15
  def content
16
- File.read(path)
16
+ file.content
17
17
  end
18
18
 
19
19
  def nil?
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ class Runner
4
+ private
5
+
6
+ attr_reader :command, :stdout
7
+
8
+ public
9
+
10
+ def initialize(command)
11
+ @command = command
12
+ end
13
+
14
+ def run
15
+ stdout, stderr, status = Open3.capture3(command)
16
+ fail RuntimeError, "Error executing command #{command}: #{stderr}, #{stdout}" unless status.success?
17
+
18
+ @stdout = stdout.split("\n")
19
+ rescue Errno::ENOENT => e
20
+ fail RuntimeError, "Error executing command #{command}: #{stderr}, #{e.message}"
21
+ end
22
+
23
+ def result
24
+ $stderr.puts command if LocalPac.debug_mode
25
+ run
26
+ stdout
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ require_relative File.expand_path('../../../spec/support/git', __FILE__)
3
+
4
+ module LocalPac
5
+ class FileServer
6
+ class SpecHelperFileServer
7
+ include LocalPac::SpecHelper::GitHelper
8
+
9
+ def initialize
10
+ LocalPac.ui_logger.level = ::Logger::UNKNOWN
11
+
12
+ config = Class.new do
13
+ include FeduxOrg::Stdlib::Filesystem
14
+
15
+ def root_directory
16
+ File.expand_path('../../../', __FILE__)
17
+ end
18
+
19
+ def local_storage
20
+ File.join(working_directory, 'git_repo', '.git')
21
+ end
22
+ end.new
23
+
24
+ LocalPac.config(config)
25
+
26
+ valid_pac_file = <<-EOS
27
+ function FindProxyForURL(url, host) {
28
+ return "DIRECT";
29
+ }
30
+ EOS
31
+ git_repo = 'git_repo'
32
+
33
+ FileUtils.rm_rf File.join(working_directory, git_repo)
34
+ git_init(git_repo)
35
+ create_file(File.join(git_repo, 'file.pac'), valid_pac_file)
36
+ git_add(git_repo, 'file.pac')
37
+ git_commit(git_repo)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ class TemplateFile
4
+
5
+ attr_reader :path
6
+
7
+ def initialize(path)
8
+ @path = path
9
+ end
10
+
11
+ def name
12
+ ::File.basename(path, '.*').to_sym
13
+ end
14
+
15
+ def read
16
+ ::File.read(path)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ module LocalPac
3
+ class TemplateRepository
4
+ private
5
+
6
+ attr_reader :root_directory, :creator
7
+
8
+ public
9
+
10
+ def initialize(root_directory = File.expand_path('../../../files', __FILE__), creator = TemplateFile)
11
+ @root_directory = root_directory
12
+ @creator = creator
13
+ end
14
+
15
+ def find(name)
16
+ path = File.join(root_directory, "#{name.to_s}.erb")
17
+ fail Exceptions::ErbTemplateIsUnknown, "Template \"#{name}\" could not be found!" unless ::File.exist? path
18
+
19
+ creator.new(path)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ module LocalPac
2
+ class UiLogger
3
+ def initialize
4
+ @logger = ::Logger.new($stderr)
5
+ end
6
+
7
+ def method_missing(method, *args, &block)
8
+ @logger.public_send method, *args
9
+ end
10
+
11
+ end
12
+ end
@@ -1,4 +1,4 @@
1
1
  #main LocalPac
2
2
  module LocalPac
3
- VERSION = '0.0.7'
3
+ VERSION = '0.1.0'
4
4
  end
data/lib/local_pac.rb CHANGED
@@ -3,16 +3,42 @@ require 'fileutils'
3
3
  require 'logger'
4
4
  require 'uglifier'
5
5
  require 'pac'
6
+ require 'pstore'
7
+ require 'yaml/store'
8
+ require 'rugged'
9
+ require 'erb'
10
+ require 'thread'
11
+ require 'open3'
12
+ require 'fedux_org/stdlib/filesystem'
6
13
 
7
14
  require 'active_support/cache'
15
+ require 'active_support/core_ext/string/inflections'
8
16
 
17
+ require 'local_pac/main'
9
18
  require 'local_pac/logger'
19
+ require 'local_pac/ui_logger'
20
+
21
+ LocalPac.ui_logger.level = Logger::UNKNOWN
22
+
23
+ require 'local_pac/git'
24
+ require 'local_pac/git_file'
25
+ require 'local_pac/git_repository'
26
+ require 'local_pac/config'
27
+ require 'local_pac/fileable'
28
+ require 'local_pac/runner'
29
+ require 'local_pac/exceptions'
30
+ require 'local_pac/erb_generator'
31
+ require 'local_pac/data'
10
32
  require 'local_pac/version'
11
- require 'local_pac/file_server'
12
- require 'local_pac/pac_manager'
13
33
  require 'local_pac/pac_file'
14
34
  require 'local_pac/null_pac_file'
15
35
  require 'local_pac/java_script_compressor'
36
+ require 'local_pac/template_repository'
37
+ require 'local_pac/template_file'
16
38
  require 'local_pac/pac_file_validator'
17
-
18
- module LocalPac; end
39
+ require 'local_pac/actions/create_repository'
40
+ require 'local_pac/actions/create_directory'
41
+ require 'local_pac/actions/create_file'
42
+ require 'local_pac/initializer'
43
+ require 'local_pac/local_storage'
44
+ require 'local_pac/file_server'
data/local_pac.gemspec CHANGED
@@ -26,4 +26,5 @@ EOS
26
26
  spec.add_runtime_dependency 'uglifier'
27
27
  spec.add_runtime_dependency 'therubyracer'
28
28
  spec.add_runtime_dependency 'pac'
29
+ spec.add_runtime_dependency 'git_hook-pre_receive', '~>0.0.2'
29
30
  end
@@ -0,0 +1,5 @@
1
+ :storage_path: '~/.local/share/pacfiles/cache/cache.pstore'
2
+ :import_paths:
3
+ - '~/.local/share/pacfiles/new'
4
+ - '~/.pacfiles/new'
5
+ - '/var/pacfiles/new'
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Actions::CreateDirectory do
5
+ context '#initialize' do
6
+ it 'requires a path' do
7
+ Actions::CreateDirectory.new(working_directory)
8
+ end
9
+ end
10
+
11
+ context '#run' do
12
+ it 'runs the action' do
13
+ action = Actions::CreateDirectory.new(File.join(working_directory, 'repo'))
14
+ silence(:stderr) do
15
+ action.run
16
+ end
17
+
18
+ expect(path_exists?('repo')).to be_true
19
+ end
20
+
21
+ it 'respects existing path' do
22
+ create_directory('repo')
23
+
24
+ action = Actions::CreateDirectory.new(File.join(working_directory, 'repo'))
25
+
26
+ result = capture(:stderr) do
27
+ LocalPac.ui_logger.level = ::Logger::INFO
28
+ action.run
29
+ end
30
+
31
+ expect(result).to include('already')
32
+ end
33
+
34
+ it 'does not respect existing path if forced to' do
35
+ create_directory('repo')
36
+
37
+ action = Actions::CreateDirectory.new(File.join(working_directory, 'repo'), force: true)
38
+
39
+ result = capture(:stderr) do
40
+ LocalPac.ui_logger.level = ::Logger::INFO
41
+ action.run
42
+ end
43
+
44
+ expect(result).to include('Creating repository')
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Actions::CreateFile do
5
+ before(:each) do
6
+ end
7
+ context '#initialize' do
8
+ it 'requires a path' do
9
+ repository = double('TemplateRepository')
10
+ data = double('Data')
11
+
12
+ engine_klass = Class.new do
13
+ def initialize(data)
14
+ end
15
+
16
+ def compile(template, destination)
17
+ end
18
+ end
19
+
20
+ Actions::CreateFile.new(:template, File.join(working_directory, 'file'), {}, data, engine_klass, repository)
21
+ end
22
+ end
23
+
24
+ context '#run' do
25
+ it 'runs the action' do
26
+ repository = double('TemplateRepository')
27
+ expect(repository).to receive(:find).with(:template)
28
+
29
+ data = double('Data')
30
+
31
+ engine_klass = Class.new do
32
+ def initialize(data)
33
+ end
34
+
35
+ def compile(template, destination)
36
+ end
37
+ end
38
+
39
+ action = Actions::CreateFile.new(:template, File.join(working_directory, 'file'), data, {}, engine_klass, repository)
40
+ silence(:stderr) do
41
+ action.run
42
+ end
43
+
44
+ expect(path_exists?('file')).to be_true
45
+ end
46
+
47
+ it 'respects existing path' do
48
+ repository = double('TemplateRepository')
49
+
50
+ data = double('Data')
51
+
52
+ engine_klass = Class.new do
53
+ def initialize(data)
54
+ end
55
+
56
+ def compile(template, destination)
57
+ end
58
+ end
59
+
60
+ file = create_file('file')
61
+ action = Actions::CreateFile.new(:template, file, data, {}, engine_klass, repository)
62
+ result = capture(:stderr) do
63
+ LocalPac.ui_logger.level = ::Logger::INFO
64
+ action.run
65
+ end
66
+
67
+ expect(result).to include('already')
68
+ end
69
+
70
+ it 'does not respect existing path if forced to' do
71
+ repository = double('TemplateRepository')
72
+ expect(repository).to receive(:find).with(:template)
73
+
74
+ data = double('Data')
75
+
76
+ engine_klass = Class.new do
77
+ def initialize(data)
78
+ end
79
+
80
+ def compile(template, destination)
81
+ end
82
+ end
83
+
84
+ file = create_file('file')
85
+ action = Actions::CreateFile.new(:template, file, data, { force: true }, engine_klass, repository)
86
+
87
+ result = capture(:stderr) do
88
+ LocalPac.ui_logger.level = ::Logger::INFO
89
+ action.run
90
+ end
91
+
92
+ expect(result).to include('Creating file')
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Actions::CreateRepository do
5
+ context '#initialize' do
6
+ it 'requires a path' do
7
+ Actions::CreateRepository.new(working_directory)
8
+ end
9
+ end
10
+
11
+ context '#run' do
12
+ it 'runs the action' do
13
+ action = Actions::CreateRepository.new(File.join(working_directory, 'repo'))
14
+ silence(:stderr) do
15
+ action.run
16
+ end
17
+
18
+ expect(path_exists?('repo')).to be_true
19
+ end
20
+
21
+ it 'respects existing path' do
22
+ create_directory('repo')
23
+
24
+ action = Actions::CreateRepository.new(File.join(working_directory, 'repo'))
25
+
26
+ result = capture(:stderr) do
27
+ LocalPac.ui_logger.level = ::Logger::INFO
28
+ action.run
29
+ end
30
+
31
+ expect(result).to include('already')
32
+ end
33
+
34
+ it 'does not respect existing path if forced to' do
35
+ create_directory('repo')
36
+
37
+ action = Actions::CreateRepository.new(File.join(working_directory, 'repo'), force: true)
38
+
39
+ result = capture(:stderr) do
40
+ LocalPac.ui_logger.level = ::Logger::INFO
41
+ action.run
42
+ end
43
+
44
+ expect(result).to include('Creating repository')
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,113 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe LocalPac::Config do
5
+ let(:config_file) { File.join(working_directory, 'config.yaml') }
6
+
7
+ context '#initialize' do
8
+ it 'requires a file name' do
9
+ LocalPac::Config.new(config_file)
10
+ end
11
+ end
12
+
13
+ context '#pid_file' do
14
+ it 'returns value of config file' do
15
+ config_file = create_file 'config.yaml', <<-EOS.strip_heredoc
16
+ ---
17
+ :pid_file: '/asdf/pid'
18
+ EOS
19
+
20
+ config = LocalPac::Config.new(config_file)
21
+ expect(config.pid_file).to eq('/asdf/pid')
22
+ end
23
+
24
+ it 'returns default value if no config file is available' do
25
+ config = LocalPac::Config.new(config_file)
26
+ file = File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'run', 'pid')
27
+
28
+ expect(config.pid_file).to eq(file)
29
+ end
30
+ end
31
+
32
+ context '#local_storage' do
33
+ it 'returns value of config file' do
34
+ config_file = create_file 'config.yaml', <<-EOS.strip_heredoc
35
+ ---
36
+ :local_storage: '/asdf/pid'
37
+ EOS
38
+
39
+ config = LocalPac::Config.new(config_file)
40
+ expect(config.local_storage).to eq('/asdf/pid')
41
+ end
42
+
43
+ it 'returns default value if no config file is available' do
44
+ config = LocalPac::Config.new(config_file)
45
+ file = File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'data')
46
+
47
+ expect(config.local_storage).to eq(file)
48
+ end
49
+ end
50
+
51
+ context '#log_sink' do
52
+ it 'returns value of config file' do
53
+ config_file = create_file 'config.yaml', <<-EOS.strip_heredoc
54
+ ---
55
+ :log_sink: '/asdf/log'
56
+ EOS
57
+
58
+ config = LocalPac::Config.new(config_file)
59
+ expect(config.log_sink).to eq('/asdf/log')
60
+ end
61
+
62
+ it 'returns default value if no config file is available' do
63
+ config = LocalPac::Config.new(config_file)
64
+ file = File.join(ENV['HOME'], '.local', 'share', 'local_pac', 'log')
65
+
66
+ expect(config.log_sink).to eq(file)
67
+ end
68
+ end
69
+
70
+ context 'executable' do
71
+ it 'returns value of config file' do
72
+ config_file = create_file 'config.yaml', <<-EOS.strip_heredoc
73
+ ---
74
+ :executable: '/asdf/bin/local_pac'
75
+ EOS
76
+
77
+ config = LocalPac::Config.new(config_file)
78
+ expect(config.executable).to eq('/asdf/bin/local_pac')
79
+ end
80
+
81
+ it 'returns default value if no config file is available' do
82
+ config = LocalPac::Config.new(config_file)
83
+ file = File.expand_path('../../bin/local_pac', __FILE__)
84
+
85
+ expect(config.executable).to eq(file)
86
+ end
87
+ end
88
+
89
+ context 'gem_path' do
90
+ it 'returns value of gem installation path file' do
91
+ config_file = create_file 'config.yaml', <<-EOS.strip_heredoc
92
+ ---
93
+ :gem_path: '/asdf/bin/local_pac'
94
+ EOS
95
+
96
+ config = LocalPac::Config.new(config_file)
97
+ expect(config.gem_path).to eq('/asdf/bin/local_pac')
98
+ end
99
+
100
+ it 'returns default value if no config file is available' do
101
+ config = LocalPac::Config.new(config_file)
102
+
103
+ expect(config.gem_path).to eq(Gem.path)
104
+ end
105
+ end
106
+
107
+ context '#to_s' do
108
+ it 'outputs a nice config overview' do
109
+ config = LocalPac::Config.new(config_file)
110
+ expect(config.to_s).to include File.join('.local', 'share', 'local_pac', 'run', 'pid')
111
+ end
112
+ end
113
+ end
data/spec/data_spec.rb ADDED
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe LocalPac::Data do
5
+ context '#instance_binding' do
6
+ it 'let you lookup variables' do
7
+ config = Class.new(Module) do
8
+ def pid_file
9
+ '/path/pid_file'
10
+ end
11
+ end.new
12
+
13
+ data = LocalPac::Data.new(config)
14
+ # rubocop:disable Eval
15
+ result = eval('lookup("pid_file")', data.instance_binding)
16
+ # rubocop:enable Eval
17
+ expect(result).to eq('/path/pid_file')
18
+ end
19
+ end
20
+
21
+ context '#lookup' do
22
+ it 'let you lookup variables' do
23
+ config = Class.new(Module) do
24
+ def pid_file
25
+ '/path/pid_file'
26
+ end
27
+ end.new
28
+
29
+ data = LocalPac::Data.new(config)
30
+ result = data.lookup('pid_file')
31
+ expect(result).to eq('/path/pid_file')
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe ErbGenerator do
5
+ context '#compile' do
6
+ it 'needs a source and a target' do
7
+ data = double('binding')
8
+ allow(data).to receive(:instance_binding) do
9
+ Class.new do
10
+ def pid_file
11
+ '/path/pid_file'
12
+ end
13
+
14
+ def instance_binding
15
+ binding
16
+ end
17
+ end.new.instance_binding
18
+ end
19
+
20
+ source = double('source')
21
+ allow(source).to receive(:encoding)
22
+ allow(source).to receive(:read).and_return('Path <%= pid_file %>')
23
+
24
+ destination = double('destination')
25
+ expect(destination).to receive(:puts).with('Path /path/pid_file')
26
+
27
+ gen = ErbGenerator.new(data)
28
+ gen.compile(source, destination)
29
+ end
30
+ end
31
+ end
@@ -2,24 +2,42 @@ require 'spec_helper'
2
2
 
3
3
  describe 'Fetch proxy pac' do
4
4
  context '/v1/pac' do
5
- let(:valid_pac_file) do <<-EOS.strip_heredoc
5
+ let(:valid_pac_file) do <<-EOS.strip_heredoc.chomp
6
6
  function FindProxyForURL(url, host) {
7
- return "DIRECT"
7
+ return "DIRECT";
8
8
  }
9
9
  EOS
10
10
  end
11
11
 
12
- it 'finds an existing proxy pac' do
13
- create_file '.config/pacfiles/file1.pac', valid_pac_file
12
+ let(:git_repo) { 'git_repo' }
14
13
 
15
- env = {
16
- 'HOME' => working_directory,
17
- }
14
+ before(:each) do
15
+ git_init(git_repo)
16
+ create_file(File.join(git_repo, 'file.pac'), valid_pac_file)
17
+ git_add(git_repo, 'file.pac')
18
+ git_commit(git_repo)
19
+ end
18
20
 
19
- with_environment env, clear: true do
20
- response = get('/v1/pac/file1.pac')
21
- expect(response.body).to eq(valid_pac_file)
22
- end
21
+ def app
22
+ config = Class.new do
23
+ include FeduxOrg::Stdlib::Filesystem
24
+
25
+ def root_directory
26
+ File.expand_path('../../../', __FILE__)
27
+ end
28
+
29
+ def local_storage
30
+ File.join(working_directory, 'git_repo', '.git')
31
+ end
32
+ end.new
33
+
34
+ LocalPac.config(config)
35
+ LocalPac::FileServer
36
+ end
37
+
38
+ it 'finds an existing proxy pac' do
39
+ response = get('/v1/pac/file.pac')
40
+ expect(response.body).to eq(valid_pac_file)
23
41
  end
24
42
 
25
43
  it 'exits with 404 if file does not exist' do
@@ -29,7 +47,7 @@ describe 'Fetch proxy pac' do
29
47
 
30
48
  it 'compresses data for transit if requested' do
31
49
  ['deflate','gzip', 'deflate,gzip','gzip,deflate'].each do|compression_method|
32
- response = get('/v1/pac/does_not_exist.pac', {}, { 'HTTP_ACCEPT_ENCODING' => compression_method })
50
+ response = get('/v1/pac/file.pac', {}, { 'HTTP_ACCEPT_ENCODING' => compression_method })
33
51
  expect(response.headers['Content-Encoding']).to be
34
52
  end
35
53
  end