mumuki-javascript-runner 1.3.4 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: abbae54f53eb61d8c168cb7205b79695911762fbd0a6152aa1f5970ab42048e6
4
- data.tar.gz: a3b1720e6d08c52c58cd5e85ea3595f40c993f4f8d9143ffc3d73b12dc155554
3
+ metadata.gz: 5584ae96f5cefe3bebfffc73f112b1e449ce5c32e3d4ff0494223c9e0b0f18e4
4
+ data.tar.gz: 92d98ddd5222726ece6dfec3a41d41c7350163d906649595db11bb8542843c7e
5
5
  SHA512:
6
- metadata.gz: e026121fce5a338302324fff1b67c69a7fd61698e70b715dc03e27e90f5741f40410995885ab16870879ad669655b1873fba0fbf03a569f4ee57c785388d5ee1
7
- data.tar.gz: 6ad1cf24cdbcb8ceb1477cc33fae853f8f9eb3502712064a26c5191308ef6b25e34dab0162fe22b1accbaa95a56996abf8dff0e78a6e66f7ff5c775bc3cce84f
6
+ metadata.gz: 5e2d6882f17a35dd4eadac042c1f34c6785900c09b37a90e2056a8bf72247869bd9a02a95137c8edb99654e9a09df168205aeecae93013ff41682182e3476739
7
+ data.tar.gz: 5a49c40554275ed469945d858f7c27aacfc7cc83e39083580959702b12d320e5d49a2c540738a1911bea9eba38a3cb0e122bd6c7b7524425531af3424c728368
@@ -0,0 +1,25 @@
1
+ class JavascriptFeedbackHook < Mumukit::Hook
2
+ def run!(request, results)
3
+ content = request.content
4
+ test_results = test_failure_messages results
5
+
6
+ JavascriptExplainer.new.explain(content, test_results)
7
+ end
8
+
9
+ def test_failure_messages(results)
10
+ structured_test_results = results.test_results[0]
11
+ if structured_test_results.is_a? Array
12
+ structured_test_results.select { |it| it[1].failed? }.map { |it| it[2] }.join("\n")
13
+ else
14
+ ''
15
+ end
16
+ end
17
+
18
+ class JavascriptExplainer < Mumukit::Explainer
19
+ def explain_is_not_defined(_, test_results)
20
+ (/([a-zA-Z0-9_]+) is not defined/.match test_results).try do |it|
21
+ {identifier: it[1]}
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,10 +1,13 @@
1
1
  require 'mumukit'
2
2
 
3
+ I18n.load_translations_path File.join(__dir__, 'locales', '*.yml')
4
+
3
5
  Mumukit.runner_name = 'javascript'
4
6
  Mumukit.configure do |config|
5
7
  config.docker_image = 'mumuki/mumuki-mocha-worker'
6
8
  config.structured = true
7
9
  config.stateful = true
10
+ config.content_type = 'markdown'
8
11
  end
9
12
 
10
13
  require_relative './version'
@@ -12,4 +15,6 @@ require_relative './metadata_hook'
12
15
  require_relative './validation_hook'
13
16
  require_relative './test_hook'
14
17
  require_relative './expectations_hook'
18
+ require_relative './feedback_hook'
15
19
  require_relative './query_hook'
20
+ require_relative './try_hook'
@@ -0,0 +1,2 @@
1
+ en:
2
+ is_not_defined: "it looks like you are using `%{identifier}` but it is not defined. Perhaps you have misspelled it or you need to define it"
@@ -0,0 +1,2 @@
1
+ es:
2
+ is_not_defined: "parece que estás usando `%{identifier}` pero no está definido. ¿Puede que hayas escrito mal su nombre o tengas que definirlo?"
@@ -0,0 +1,2 @@
1
+ pt:
2
+ is_not_defined: "Parece que você usa %{identifier} mas não está indefinido. Você digitou o nome errado ou precisa defini-lo"
data/lib/query_hook.rb CHANGED
@@ -1,18 +1,27 @@
1
1
  class JavascriptQueryHook < Mumukit::Templates::FileHook
2
+ with_error_patterns
2
3
  isolated true
3
4
 
5
+ VAR_REGEXP = /^ *(?:var|let) +([a-zA-Z_$][a-zA-Z_$0-9]*)/
6
+ VAR_ASSIGN_REGEXP = /^ *(?:var|let) +([a-zA-Z_$][a-zA-Z_$0-9]*) *=/
7
+ CONST_ASSIGN_REGEXP = /^ *const +([a-zA-Z_$][a-zA-Z_$0-9]*) *=/
8
+
4
9
  def tempfile_extension
5
10
  '.js'
6
11
  end
7
12
 
8
-
9
13
  def compile_file_content(r)
14
+ "#{compile_file_header(r)}\n#{compile_query(r.query)}"
15
+ end
16
+
17
+ def compile_file_header(r)
10
18
  <<javascript
11
19
  'use strict';
12
20
 
13
21
  function mumukiConsolePrettyPrint(e) {
14
22
  if (e instanceof Function) return '<function>';
15
- return JSON.stringify(e);
23
+ const json = JSON.stringify(e);
24
+ return json && json.replace(/"(\\w+)"\s*:/g, '$1:');
16
25
  }
17
26
 
18
27
  #{r.extra}
@@ -20,25 +29,81 @@ function mumukiConsolePrettyPrint(e) {
20
29
  #{r.content}
21
30
 
22
31
  #{compile_cookie(r.cookie)}
23
-
24
- #{compile_query(r.query)}
25
32
  javascript
26
33
  end
27
34
 
28
- def compile_query(query)
29
- if query.start_with?('var ') || query.start_with?('let ')
30
- "#{query}\nconsole.log('=> undefined')"
35
+ def compile_query(query, output_prefix = "=> ", output_var = "__mumuki_query_result__")
36
+ if ['var', 'let', 'const'].any? { |type| query.start_with? "#{type} " }
37
+ "#{query}\nconsole.log('#{output_prefix}undefined')"
31
38
  else
32
- "var __mumuki_query_result__ = #{query};\nconsole.log('=> ' + mumukiConsolePrettyPrint(__mumuki_query_result__))"
39
+ "var #{output_var} = #{query};\nconsole.log('#{output_prefix}' + mumukiConsolePrettyPrint(#{output_var}))"
33
40
  end
34
41
  end
35
42
 
36
43
  def compile_cookie(cookie)
37
44
  return if cookie.blank?
38
- cookie.map { |query| "try { #{query} } catch (e) {}" }.join("\n")
45
+
46
+ compile_statements(cookie).join "\n"
39
47
  end
40
48
 
41
49
  def command_line(filename)
42
50
  "node #{filename}"
43
51
  end
52
+
53
+ private
54
+
55
+ def compile_statements(cookie)
56
+ reject_duplicated_statements wrap_statements(cookie)
57
+ end
58
+
59
+ def wrap_statements(cookie)
60
+ cookie.map do |query|
61
+ case query
62
+ when CONST_ASSIGN_REGEXP
63
+ declaration_with_assignment 'const', CONST_ASSIGN_REGEXP, $1, query
64
+ when VAR_ASSIGN_REGEXP
65
+ declaration_with_assignment 'var', VAR_ASSIGN_REGEXP, $1, query
66
+ when VAR_REGEXP
67
+ "var #{$1}"
68
+ else
69
+ "try { #{query} } catch (e) {}"
70
+ end
71
+ end
72
+ end
73
+
74
+ def reject_duplicated_statements(sentences)
75
+ sentences.select.with_index do |line, index|
76
+ next line if !line.match(VAR_ASSIGN_REGEXP) && !line.match(VAR_REGEXP) && !line.match(CONST_ASSIGN_REGEXP)
77
+ name = $1
78
+ sentences.slice(0, index).none? { |previous_line| declaration? previous_line, name }
79
+ end
80
+ end
81
+
82
+ def declaration_with_assignment(type, type_pattern, name, expression)
83
+ "#{type} #{name} = (function() { try { return #{expression.gsub(type_pattern, '')} } catch(e) { return undefined } })()"
84
+ end
85
+
86
+ def declaration?(line, name)
87
+ declaration_of_type?(VAR_REGEXP, line, name) ||
88
+ declaration_of_type?(VAR_ASSIGN_REGEXP, line, name) ||
89
+ declaration_of_type?(CONST_ASSIGN_REGEXP, line, name)
90
+ end
91
+
92
+ def declaration_of_type?(type_pattern, line, name)
93
+ line.match(type_pattern) && $1 == name
94
+ end
95
+
96
+ def error_patterns
97
+ [
98
+ Mumukit::ErrorPattern::Errored.new(error_regexp)
99
+ ]
100
+ end
101
+
102
+ def error_types
103
+ '(Reference|Syntax|Type)Error'
104
+ end
105
+
106
+ def error_regexp
107
+ /(?=\X*#{error_types})(solution.*\n|var __mumuki_query_result__ = )|#{error_types}.*\n\K\X*/
108
+ end
44
109
  end
data/lib/test_hook.rb CHANGED
@@ -1,13 +1,12 @@
1
1
  class JavascriptTestHook < Mumukit::Templates::FileHook
2
2
  isolated true
3
3
  structured true, separator: '!!!JAVASCRIPT-MUMUKI-OUTPUT!!!'
4
+ line_number_offset 3, include_extra: true
4
5
 
5
6
  def compile_file_content(request)
6
7
  <<javascript
7
8
  'use strict';
8
-
9
- var assert = require('assert');
10
-
9
+ let assert = require('assert');
11
10
  #{request.extra}
12
11
  #{request.content}
13
12
  describe('', function() {
@@ -32,11 +31,19 @@ javascript
32
31
  transform(super['tests'])
33
32
  end
34
33
 
34
+ def post_process_unstructured_result(file, result, status)
35
+ if status.errored?
36
+ [content_type.code(result.gsub(/(SyntaxError: .*\n)(.|\n)*/) { $1 }), status]
37
+ else
38
+ super
39
+ end
40
+ end
41
+
35
42
  def transform(examples)
36
43
  examples.map { |e| [e['fullTitle'], e['err'].present? ? :failed : :passed, parse_out(e['err'])] }
37
44
  end
38
45
 
39
46
  def parse_out(exception)
40
- exception.present? ? exception['message'] : ''
47
+ exception.present? ? content_type.code(exception['message']) : ''
41
48
  end
42
49
  end
data/lib/try_hook.rb ADDED
@@ -0,0 +1,48 @@
1
+ class JavascriptTryHook < Mumukit::Templates::TryHook
2
+ isolated true
3
+ attr_reader :query_hook
4
+
5
+ def initialize(config = nil)
6
+ super config
7
+ @query_hook = JavascriptQueryHook.new
8
+ end
9
+
10
+ def compile_file_content(r)
11
+ <<js
12
+ #{@query_hook.compile_file_header(r)}
13
+ console.log("#{query_separator}");
14
+ #{@query_hook.compile_query(r.query, '')}
15
+ console.log("#{goal_separator}");
16
+ #{@query_hook.compile_query(r.goal.with_indifferent_access[:query] || 'null', '', '__mumuki_goal_query_result__')}
17
+ js
18
+ end
19
+
20
+ delegate :tempfile_extension, to: :query_hook
21
+ delegate :command_line, to: :query_hook
22
+
23
+ def query_separator
24
+ '!!!MUMUKI-QUERY-START!!!'
25
+ end
26
+
27
+ def goal_separator
28
+ '!!!MUMUKI-GOAL-START!!!'
29
+ end
30
+
31
+ def to_structured_results(_file, result, status)
32
+ /#{query_separator}
33
+ ?(.*)
34
+ #{goal_separator}
35
+ ?(.*)
36
+ /m =~ result
37
+
38
+ {
39
+ query: to_query_result($1, status),
40
+ goal: $2,
41
+ status: status
42
+ }
43
+ end
44
+
45
+ def to_query_result(result, status)
46
+ { result: result, status: status }
47
+ end
48
+ end
@@ -1,7 +1,7 @@
1
1
  class JavascriptValidationHook < Mumukit::Hook
2
2
  def validate!(request)
3
3
  matches = request_matches?(request) do |it|
4
- it =~ /(^|\W+)(require|process|os|fs|eval|cluster|v8|vm|tty|tls|root|global|crypto|stream|events)(\W+|$)/
4
+ it =~ /(^|[^_[[:alnum:]]]+)(require|process|os|fs|eval|cluster|v8|vm|tty|tls|root|global|crypto|stream|events)([^_[[:alnum:]]]+|$)/
5
5
  end
6
6
  raise Mumukit::RequestValidationError, "You can not use #{$2} here" if matches
7
7
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module JavascriptVersionHook
2
- VERSION = '1.3.4'
2
+ VERSION = '1.7.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumuki-javascript-runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franco Leonardo Bulgarelli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-29 00:00:00.000000000 Z
11
+ date: 2021-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mumukit
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.20'
19
+ version: '2.40'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.20'
26
+ version: '2.40'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.7'
33
+ version: '2.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.7'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -102,10 +102,15 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - lib/expectations_hook.rb
105
+ - lib/feedback_hook.rb
105
106
  - lib/javascript_runner.rb
107
+ - lib/locales/en.yml
108
+ - lib/locales/es.yml
109
+ - lib/locales/pt.yml
106
110
  - lib/metadata_hook.rb
107
111
  - lib/query_hook.rb
108
112
  - lib/test_hook.rb
113
+ - lib/try_hook.rb
109
114
  - lib/validation_hook.rb
110
115
  - lib/version.rb
111
116
  homepage: http://github.com/mumuki/mumuki-javascript-server
@@ -127,8 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
132
  - !ruby/object:Gem::Version
128
133
  version: '0'
129
134
  requirements: []
130
- rubyforge_project:
131
- rubygems_version: 2.7.7
135
+ rubygems_version: 3.0.3
132
136
  signing_key:
133
137
  specification_version: 4
134
138
  summary: Javascript Runner for Mumuki