iruby 0.2.9 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ubuntu.yml +62 -0
  3. data/CHANGES +64 -0
  4. data/Gemfile +3 -1
  5. data/LICENSE +1 -1
  6. data/README.md +120 -92
  7. data/Rakefile +36 -10
  8. data/ci/Dockerfile.base.erb +41 -0
  9. data/ci/Dockerfile.main.erb +7 -0
  10. data/ci/requirements.txt +1 -0
  11. data/docker/setup.sh +15 -0
  12. data/docker/test.sh +7 -0
  13. data/iruby.gemspec +13 -18
  14. data/lib/iruby.rb +13 -6
  15. data/lib/iruby/backend.rb +38 -9
  16. data/lib/iruby/command.rb +68 -12
  17. data/lib/iruby/display.rb +81 -41
  18. data/lib/iruby/event_manager.rb +40 -0
  19. data/lib/iruby/formatter.rb +3 -3
  20. data/lib/iruby/input.rb +6 -6
  21. data/lib/iruby/input/README.ipynb +55 -3
  22. data/lib/iruby/input/README.md +299 -0
  23. data/lib/iruby/input/autoload.rb +3 -2
  24. data/lib/iruby/input/builder.rb +4 -4
  25. data/lib/iruby/input/button.rb +2 -2
  26. data/lib/iruby/input/cancel.rb +1 -1
  27. data/lib/iruby/input/checkbox.rb +22 -4
  28. data/lib/iruby/input/date.rb +17 -6
  29. data/lib/iruby/input/field.rb +5 -4
  30. data/lib/iruby/input/file.rb +3 -3
  31. data/lib/iruby/input/form.rb +6 -6
  32. data/lib/iruby/input/label.rb +9 -3
  33. data/lib/iruby/input/multiple.rb +76 -0
  34. data/lib/iruby/input/popup.rb +5 -2
  35. data/lib/iruby/input/radio.rb +18 -6
  36. data/lib/iruby/input/select.rb +26 -12
  37. data/lib/iruby/input/textarea.rb +2 -1
  38. data/lib/iruby/input/widget.rb +2 -2
  39. data/lib/iruby/jupyter.rb +77 -0
  40. data/lib/iruby/kernel.rb +171 -31
  41. data/lib/iruby/ostream.rb +29 -8
  42. data/lib/iruby/session.rb +116 -0
  43. data/lib/iruby/session/{rbczmq.rb → cztop.rb} +21 -19
  44. data/lib/iruby/session/ffi_rzmq.rb +1 -1
  45. data/lib/iruby/session_adapter.rb +72 -0
  46. data/lib/iruby/session_adapter/cztop_adapter.rb +45 -0
  47. data/lib/iruby/session_adapter/ffirzmq_adapter.rb +55 -0
  48. data/lib/iruby/session_adapter/pyzmq_adapter.rb +77 -0
  49. data/lib/iruby/session_adapter/test_adapter.rb +49 -0
  50. data/lib/iruby/utils.rb +13 -2
  51. data/lib/iruby/version.rb +1 -1
  52. data/run-test.sh +12 -0
  53. data/tasks/ci.rake +65 -0
  54. data/test/helper.rb +133 -0
  55. data/test/integration_test.rb +22 -11
  56. data/test/iruby/backend_test.rb +37 -0
  57. data/test/iruby/command_test.rb +207 -0
  58. data/test/iruby/event_manager_test.rb +92 -0
  59. data/test/iruby/jupyter_test.rb +27 -0
  60. data/test/iruby/kernel_test.rb +153 -0
  61. data/test/iruby/mime_test.rb +43 -0
  62. data/test/iruby/multi_logger_test.rb +1 -2
  63. data/test/iruby/session_adapter/cztop_adapter_test.rb +20 -0
  64. data/test/iruby/session_adapter/ffirzmq_adapter_test.rb +20 -0
  65. data/test/iruby/session_adapter/session_adapter_test_base.rb +27 -0
  66. data/test/iruby/session_adapter_test.rb +91 -0
  67. data/test/iruby/session_test.rb +48 -0
  68. data/test/run-test.rb +19 -0
  69. metadata +107 -50
  70. data/.travis.yml +0 -16
  71. data/CONTRIBUTORS +0 -19
  72. data/test/test_helper.rb +0 -5
@@ -0,0 +1,49 @@
1
+ require 'iruby/session/mixin'
2
+
3
+ module IRuby
4
+ module SessionAdapter
5
+ class TestAdapter < BaseAdapter
6
+ include IRuby::SessionSerialize
7
+
8
+ DummySocket = Struct.new(:type, :protocol, :host, :port)
9
+
10
+ def initialize(config)
11
+ super
12
+
13
+ unless config['key'].empty? || config['signature_scheme'].empty?
14
+ unless config['signature_scheme'] =~ /\Ahmac-/
15
+ raise "Unknown signature_scheme: #{config['signature_scheme']}"
16
+ end
17
+ digest_algorithm = config['signature_scheme'][/\Ahmac-(.*)\Z/, 1]
18
+ @hmac = OpenSSL::HMAC.new(config['key'], OpenSSL::Digest.new(digest_algorithm))
19
+ end
20
+
21
+ @send_callback = nil
22
+ @recv_callback = nil
23
+ end
24
+
25
+ attr_accessor :send_callback, :recv_callback
26
+
27
+ def send(sock, data)
28
+ unless @send_callback.nil?
29
+ @send_callback.call(sock, unserialize(data))
30
+ end
31
+ end
32
+
33
+ def recv(sock)
34
+ unless @recv_callback.nil?
35
+ serialize(@recv_callback.call(sock))
36
+ end
37
+ end
38
+
39
+ def heartbeat_loop(sock)
40
+ end
41
+
42
+ private
43
+
44
+ def make_socket(type, protocol, host, port)
45
+ DummySocket.new(type, protocol, host, port)
46
+ end
47
+ end
48
+ end
49
+ end
data/lib/iruby/utils.rb CHANGED
@@ -4,34 +4,45 @@ module IRuby
4
4
  Display.convert(object, options)
5
5
  end
6
6
 
7
+ # Display the object
7
8
  def display(obj, options = {})
8
9
  Kernel.instance.session.send(:publish, :display_data,
9
10
  data: Display.display(obj, options),
10
- metadata: {},
11
- source: 'ruby') unless obj.nil?
11
+ metadata: {}) unless obj.nil?
12
12
  end
13
13
 
14
+ # Clear the output area
15
+ def clear_output(wait=false)
16
+ Display.clear_output(wait)
17
+ end
18
+
19
+ # Format the given object into HTML table
14
20
  def table(s, **options)
15
21
  html(HTML.table(s, options))
16
22
  end
17
23
 
24
+ # Treat the given string as LaTeX text
18
25
  def latex(s)
19
26
  convert(s, mime: 'text/latex')
20
27
  end
21
28
  alias tex latex
22
29
 
30
+ # Format the given string of TeX equation into LaTeX text
23
31
  def math(s)
24
32
  convert("$$#{s}$$", mime: 'text/latex')
25
33
  end
26
34
 
35
+ # Treat the given string as HTML
27
36
  def html(s)
28
37
  convert(s, mime: 'text/html')
29
38
  end
30
39
 
40
+ # Treat the given string as JavaScript code
31
41
  def javascript(s)
32
42
  convert(s, mime: 'application/javascript')
33
43
  end
34
44
 
45
+ # Treat the given string as SVG text
35
46
  def svg(s)
36
47
  convert(s, mime: 'image/svg+xml')
37
48
  end
data/lib/iruby/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module IRuby
2
- VERSION = '0.2.9'
2
+ VERSION = '0.6.1'
3
3
  end
data/run-test.sh ADDED
@@ -0,0 +1,12 @@
1
+ #! /bin/bash
2
+
3
+ set -ex
4
+
5
+ export PYTHON=python3
6
+
7
+ ADAPTERS="cztop ffi-rzmq pyzmq"
8
+
9
+ for adapter in $ADAPTERS; do
10
+ export IRUBY_TEST_SESSION_ADAPTER_NAME=$adapter
11
+ bundle exec rake test TESTOPTS=-v
12
+ done
data/tasks/ci.rake ADDED
@@ -0,0 +1,65 @@
1
+ namespace :ci do
2
+ namespace :docker do
3
+ def ruby_version
4
+ @ruby_version ||= ENV['ruby_version']
5
+ end
6
+
7
+ def ruby_image_name
8
+ @ruby_image_name ||= "rubylang/ruby:#{ruby_version}-bionic"
9
+ end
10
+
11
+ def iruby_test_base_image_name
12
+ @iruby_test_base_image_name ||= "iruby-test-base:ruby-#{ruby_version}"
13
+ end
14
+
15
+ def iruby_test_image_name
16
+ @docker_image_name ||= begin
17
+ "sciruby/iruby-test:ruby-#{ruby_version}"
18
+ end
19
+ end
20
+
21
+ def docker_image_found?(image_name)
22
+ image_id = `docker images -q #{image_name}`.chomp
23
+ image_id.length > 0
24
+ end
25
+
26
+ directory 'tmp'
27
+
28
+ desc "Build iruby-test-base docker image"
29
+ task :build_test_base_image => 'tmp' do
30
+ unless docker_image_found?(iruby_test_base_image_name)
31
+ require 'erb'
32
+ dockerfile_content = ERB.new(File.read('ci/Dockerfile.base.erb')).result(binding)
33
+ File.write('tmp/Dockerfile', dockerfile_content)
34
+ sh 'docker', 'build', '-t', iruby_test_base_image_name, '-f', 'tmp/Dockerfile', '.'
35
+ end
36
+ end
37
+
38
+ desc "Pull docker image of ruby"
39
+ task :pull_ruby_image do
40
+ sh 'docker', 'pull', ruby_image_name
41
+ end
42
+
43
+ desc "Build iruby-test docker image"
44
+ task :build_test_image => 'tmp' do
45
+ require 'erb'
46
+ dockerfile_content = ERB.new(File.read('ci/Dockerfile.main.erb')).result(binding)
47
+ File.write('tmp/Dockerfile', dockerfile_content)
48
+ sh 'docker', 'build', '-t', iruby_test_image_name, '-f', 'tmp/Dockerfile', '.'
49
+ end
50
+
51
+ desc 'before_install script for CI with Docker'
52
+ task :before_install => :pull_ruby_image
53
+ task :before_install => :build_test_base_image
54
+
55
+ desc 'install script for CI with Docker'
56
+ task :install => :build_test_image
57
+
58
+ desc 'main script for CI with Docker'
59
+ task :script do
60
+ volumes = ['-v', "#{Dir.pwd}:/iruby"] if ENV['attach_pwd']
61
+ sh 'docker', 'run', '--rm', '-it', *volumes,
62
+ iruby_test_image_name, 'bash', 'run-test.sh'
63
+ end
64
+ end
65
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,133 @@
1
+ require "iruby"
2
+ require "iruby/logger"
3
+ require "json"
4
+ require 'multi_json'
5
+ require "pathname"
6
+ require "test/unit"
7
+ require "test/unit/rr"
8
+ require "tmpdir"
9
+
10
+
11
+ IRuby.logger = IRuby::MultiLogger.new(*Logger.new(STDERR, level: Logger::Severity::INFO))
12
+
13
+ module IRubyTest
14
+ class TestBase < Test::Unit::TestCase
15
+ def self.startup
16
+ @__config_dir = Dir.mktmpdir("iruby-test")
17
+ @__config_path = Pathname.new(@__config_dir) + "config.json"
18
+ File.write(@__config_path, {
19
+ control_port: 50160,
20
+ shell_port: 57503,
21
+ transport: "tcp",
22
+ signature_scheme: "hmac-sha256",
23
+ stdin_port: 52597,
24
+ hb_port: 42540,
25
+ ip: "127.0.0.1",
26
+ iopub_port: 40885,
27
+ key: "a0436f6c-1916-498b-8eb9-e81ab9368e84"
28
+ }.to_json)
29
+
30
+ @__original_kernel_instance = IRuby::Kernel.instance
31
+ end
32
+
33
+ def self.shutdown
34
+ FileUtils.remove_entry_secure(@__config_dir)
35
+ end
36
+
37
+ def self.test_config_filename
38
+ @__config_path.to_s
39
+ end
40
+
41
+ def teardown
42
+ IRuby::Kernel.instance = @__original_kernel_instance
43
+ end
44
+
45
+ def with_session_adapter(session_adapter_name)
46
+ IRuby::Kernel.new(self.class.test_config_filename, session_adapter_name)
47
+ $stdout = STDOUT
48
+ $stderr = STDERR
49
+ end
50
+
51
+ def assert_output(stdout=nil, stderr=nil)
52
+ flunk "assert_output requires a block to capture output." unless block_given?
53
+
54
+ out, err = capture_io do
55
+ yield
56
+ end
57
+
58
+ y = check_assert_output_result(stderr, err, "stderr")
59
+ x = check_assert_output_result(stdout, out, "stdout")
60
+
61
+ (!stdout || x) && (!stderr || y)
62
+ end
63
+
64
+ private
65
+
66
+ def capture_io
67
+ captured_stdout = StringIO.new
68
+ captured_stderr = StringIO.new
69
+
70
+ orig_stdout, $stdout = $stdout, captured_stdout
71
+ orig_stderr, $stderr = $stderr, captured_stderr
72
+
73
+ yield
74
+
75
+ return captured_stdout.string, captured_stderr.string
76
+ ensure
77
+ $stdout = orig_stdout
78
+ $stderr = orig_stderr
79
+ end
80
+
81
+ def check_assert_output_result(expected, actual, name)
82
+ if expected
83
+ message = "In #{name}"
84
+ case expected
85
+ when Regexp
86
+ assert_match(expected, actual, message)
87
+ else
88
+ assert_equal(expected, actual, message)
89
+ end
90
+ end
91
+ end
92
+
93
+ def ignore_warning
94
+ saved, $VERBOSE = $VERBOSE , nil
95
+ yield
96
+ ensure
97
+ $VERBOSE = saved
98
+ end
99
+
100
+ def with_env(env)
101
+ keys = env.keys
102
+ saved_values = ENV.values_at(*keys)
103
+ ENV.update(env)
104
+ yield
105
+ ensure
106
+ if keys && saved_values
107
+ keys.zip(saved_values) do |k, v|
108
+ ENV[k] = v
109
+ end
110
+ end
111
+ end
112
+
113
+ def windows_only
114
+ omit('windows only test') unless windows?
115
+ end
116
+
117
+ def apple_only
118
+ omit('apple only test') unless windows?
119
+ end
120
+
121
+ def unix_only
122
+ omit('unix only test') if windows? || apple?
123
+ end
124
+
125
+ def windows?
126
+ /mingw|mswin/ =~ RUBY_PLATFORM
127
+ end
128
+
129
+ def apple?
130
+ /darwin/ =~ RUBY_PLATFORM
131
+ end
132
+ end
133
+ end
@@ -1,40 +1,51 @@
1
- require 'test_helper'
2
- require 'open3'
1
+ require 'pty'
3
2
  require 'expect'
4
3
 
5
- class IntegrationTest < IRubyTest
4
+ class IRubyTest::IntegrationTest < IRubyTest::TestBase
6
5
  def setup
7
- @stdin, @stdout, @stderr, @process = Open3.popen3('bin/iruby')
6
+ $expect_verbose = false # make true if you want to dump the output of iruby console
7
+
8
+ @in, @out, pid = PTY.spawn('bin/iruby --simple-prompt')
9
+ @waiter = Thread.start { Process.waitpid(pid) }
8
10
  expect 'In [', 30
9
11
  expect '1'
10
12
  expect ']:'
11
13
  end
12
14
 
13
15
  def teardown
14
- @stdin.close
15
- @stdout.close
16
- @stderr.close
17
- @process.kill
16
+ @in.close
17
+ @out.close
18
+ @waiter.join
18
19
  end
19
20
 
20
21
  def write(input)
21
- @stdin.puts input
22
+ @out.puts input
22
23
  end
23
24
 
24
- def expect(pattern, timeout = 1)
25
- assert @stdout.expect(pattern, timeout), "#{pattern} expected"
25
+ def expect(pattern, timeout = 10)
26
+ assert @in.expect(pattern, timeout), "#{pattern} expected, but timeout"
27
+ end
28
+
29
+ def wait_prompt
30
+ expect 'In ['
31
+ expect ']:'
26
32
  end
27
33
 
28
34
  def test_interaction
35
+ omit("This test too much unstable")
36
+
29
37
  write '"Hello, world!"'
30
38
  expect '"Hello, world!"'
31
39
 
40
+ wait_prompt
32
41
  write 'puts "Hello!"'
33
42
  expect 'Hello!'
34
43
 
44
+ wait_prompt
35
45
  write '12 + 12'
36
46
  expect '24'
37
47
 
48
+ wait_prompt
38
49
  write 'ls'
39
50
  expect 'self.methods'
40
51
  end
@@ -0,0 +1,37 @@
1
+ module IRubyTest
2
+ class PlainBackendTest < IRubyTest::TestBase
3
+ def setup
4
+ @plainbackend = IRuby::PlainBackend.new
5
+ end
6
+
7
+ def test_eval_one_plus_two
8
+ assert_equal 3, @plainbackend.eval('1+2', false)
9
+ end
10
+
11
+ def test_complete_req
12
+ assert_includes @plainbackend.complete('req'), 'require'
13
+ end
14
+
15
+ def test_complete_2_dot
16
+ assert_includes @plainbackend.complete('2.'), '2.even?'
17
+ end
18
+ end
19
+
20
+ class PryBackendTest < IRubyTest::TestBase
21
+ def setup
22
+ @prybackend = IRuby::PryBackend.new
23
+ end
24
+
25
+ def test_eval_one_plus_two
26
+ assert_equal 3, @prybackend.eval('1+2', false)
27
+ end
28
+
29
+ def test_complete_req
30
+ assert_includes @prybackend.complete('req'), 'require'
31
+ end
32
+
33
+ def test_complete_2_dot
34
+ assert_includes @prybackend.complete('2.'), '2.even?'
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,207 @@
1
+ require 'iruby/command'
2
+
3
+ module IRubyTest
4
+ class CommandTest < TestBase
5
+ def test_with_empty_argv
6
+ with_env('JUPYTER_DATA_DIR' => nil,
7
+ 'IPYTHONDIR' => nil) do
8
+ assert_output(nil, /\A\z/) do
9
+ @command = IRuby::Command.new([])
10
+ kernel_dir = File.join(IRuby::Jupyter.kernelspec_dir, 'ruby')
11
+ assert_equal(kernel_dir, @command.kernel_dir)
12
+ assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
13
+ end
14
+ end
15
+ end
16
+
17
+ def test_with_JUPYTER_DATA_DIR
18
+ Dir.mktmpdir do |tmpdir|
19
+ with_env('JUPYTER_DATA_DIR' => tmpdir,
20
+ 'IPYTHONDIR' => nil) do
21
+ assert_output(nil, /\A\z/) do
22
+ @command = IRuby::Command.new([])
23
+ kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
24
+ assert_equal(kernel_dir, @command.kernel_dir)
25
+ assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def test_with_IPYTHONDIR
32
+ Dir.mktmpdir do |tmpdir|
33
+ with_env('JUPYTER_DATA_DIR' => nil,
34
+ 'IPYTHONDIR' => tmpdir) do
35
+ assert_output(nil, /IPYTHONDIR is deprecated\. Use JUPYTER_DATA_DIR instead\./) do
36
+ @command = IRuby::Command.new([])
37
+ kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
38
+ assert_equal(kernel_dir, @command.kernel_dir)
39
+ assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def test_with_JUPYTER_DATA_DIR_and_IPYTHONDIR
46
+ Dir.mktmpdir do |tmpdir|
47
+ Dir.mktmpdir do |tmpdir2|
48
+ with_env('JUPYTER_DATA_DIR' => tmpdir,
49
+ 'IPYTHONDIR' => tmpdir2) do
50
+ assert_output(nil, /both JUPYTER_DATA_DIR and IPYTHONDIR are supplied; IPYTHONDIR is ignored\./) do
51
+ @command = IRuby::Command.new([])
52
+ kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
53
+ assert_equal(kernel_dir, @command.kernel_dir)
54
+ assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def test_with_ipython_dir_option
62
+ Dir.mktmpdir do |tmpdir|
63
+ with_env('JUPYTER_DATA_DIR' => nil,
64
+ 'IPYTHONDIR' => nil) do
65
+ assert_output(nil, /--ipython-dir is deprecated\. Use JUPYTER_DATA_DIR environment variable instead\./) do
66
+ @command = IRuby::Command.new(%W[--ipython-dir=#{tmpdir}])
67
+ kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
68
+ assert_equal(kernel_dir, @command.kernel_dir)
69
+ assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ def test_with_JUPYTER_DATA_DIR_and_ipython_dir_option
76
+ Dir.mktmpdir do |tmpdir|
77
+ Dir.mktmpdir do |tmpdir2|
78
+ with_env('JUPYTER_DATA_DIR' => tmpdir,
79
+ 'IPYTHONDIR' => nil) do
80
+ assert_output(nil, /both JUPYTER_DATA_DIR and --ipython-dir are supplied; --ipython-dir is ignored\./) do
81
+ @command = IRuby::Command.new(%W[--ipython-dir=#{tmpdir2}])
82
+ kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
83
+ assert_equal(kernel_dir, @command.kernel_dir)
84
+ assert_equal(File.join(kernel_dir, 'kernel.json'), @command.kernel_file)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ def test_register_when_there_is_kernel_in_ipython_dir
92
+ Dir.mktmpdir do |tmpdir|
93
+ Dir.mktmpdir do |tmpdir2|
94
+ with_env('JUPYTER_DATA_DIR' => nil,
95
+ 'IPYTHONDIR' => nil,
96
+ 'HOME' => tmpdir2) do
97
+ ignore_warning do
98
+ @command = IRuby::Command.new(["register", "--ipython-dir=~/.ipython"])
99
+ assert_equal("#{tmpdir2}/.ipython/kernels/ruby/kernel.json", @command.kernel_file)
100
+ @command.run
101
+ assert(File.file?("#{tmpdir2}/.ipython/kernels/ruby/kernel.json"))
102
+ end
103
+ end
104
+
105
+ with_env('JUPYTER_DATA_DIR' => nil,
106
+ 'IPYTHONDIR' => nil,
107
+ 'HOME' => tmpdir2) do
108
+ @command = IRuby::Command.new(["register"])
109
+ assert_output(nil, /IRuby kernel file already exists in the deprecated IPython's data directory\.\nUsing --force, you can replace the old kernel file with the new one in Jupyter's data directory\./) do
110
+ @command.run
111
+ end
112
+ assert(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
113
+ refute(File.file?(@command.kernel_file))
114
+
115
+ @command = IRuby::Command.new(["register", "--force"])
116
+ assert_output(nil, nil) do
117
+ @command.run
118
+ end
119
+ refute(File.exist?(@command.ipython_kernel_dir))
120
+ assert(File.file?(@command.kernel_file))
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ def test_register_and_unregister_with_JUPYTER_DATA_DIR
127
+ Dir.mktmpdir do |tmpdir|
128
+ Dir.mktmpdir do |tmpdir2|
129
+ with_env('JUPYTER_DATA_DIR' => tmpdir,
130
+ 'IPYTHONDIR' => nil,
131
+ 'HOME' => tmpdir2) do
132
+ assert_output(nil, nil) do
133
+ @command = IRuby::Command.new(['register'])
134
+ kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
135
+ kernel_file = File.join(kernel_dir, 'kernel.json')
136
+ assert(!File.file?(kernel_file))
137
+
138
+ @command.run
139
+ assert(File.file?(kernel_file))
140
+
141
+ @command = IRuby::Command.new(['unregister'])
142
+ @command.run
143
+ assert(!File.file?(kernel_file))
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+
150
+ def test_register_and_unregister_with_JUPYTER_DATA_DIR_when_there_is_kernel_in_ipython_dir
151
+ Dir.mktmpdir do |tmpdir|
152
+ Dir.mktmpdir do |tmpdir2|
153
+ with_env('HOME' => tmpdir2) do
154
+ ignore_warning do
155
+ @command = IRuby::Command.new(["register", "--ipython-dir=~/.ipython"])
156
+ assert_equal("#{tmpdir2}/.ipython/kernels/ruby/kernel.json", @command.kernel_file)
157
+ @command.run
158
+ assert(File.file?("#{tmpdir2}/.ipython/kernels/ruby/kernel.json"))
159
+ end
160
+ end
161
+
162
+ with_env('JUPYTER_DATA_DIR' => tmpdir,
163
+ 'IPYTHONDIR' => nil,
164
+ 'HOME' => tmpdir2) do
165
+ @command = IRuby::Command.new(["register"])
166
+ assert_output(nil, /IRuby kernel file already exists in the deprecated IPython's data directory\.\nUsing --force, you can replace the old kernel file with the new one in Jupyter's data directory\./) do
167
+ @command.run
168
+ end
169
+ assert(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
170
+ refute(File.file?(@command.kernel_file))
171
+
172
+ @command = IRuby::Command.new(["register", "--force"])
173
+ assert_output(nil, nil) do
174
+ @command.run
175
+ end
176
+ refute(File.file?(File.join(@command.ipython_kernel_dir, 'kernel.json')))
177
+ assert(File.file?(@command.kernel_file))
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ def test_register_and_unregister_with_IPYTHONDIR
184
+ Dir.mktmpdir do |tmpdir|
185
+ Dir.mktmpdir do |tmpdir2|
186
+ with_env('JUPYTER_DATA_DIR' => nil,
187
+ 'IPYTHONDIR' => tmpdir,
188
+ 'HOME' => tmpdir2) do
189
+ ignore_warning do
190
+ @command = IRuby::Command.new(['register'])
191
+ kernel_dir = File.join(tmpdir, 'kernels', 'ruby')
192
+ kernel_file = File.join(kernel_dir, 'kernel.json')
193
+ assert(!File.file?(kernel_file))
194
+
195
+ @command.run
196
+ assert(File.file?(kernel_file))
197
+
198
+ @command = IRuby::Command.new(['unregister'])
199
+ @command.run
200
+ assert(!File.file?(kernel_file))
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end