Skip to content

Commit 0ff4b9d

Browse files
committed
Test JSON output format.
- Adds JSON Schema. - Slight changes to output generation. - `Message.source_lines` can be `null`.
1 parent 5d18786 commit 0ff4b9d

File tree

5 files changed

+103
-13
lines changed

5 files changed

+103
-13
lines changed

lib/log_parser/log_parser.rb

-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ def consume_pattern(pattern)
168168
message.log_lines = { from: @log_line_number,
169169
to: @log_line_number + consumed_lines - 1 }
170170
message.source_file ||= @files.last
171-
message.source_lines ||= { from: nil, to: nil }
172171

173172
Logger.debug message
174173
remove_consumed_lines consumed_lines

lib/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# @attr [String] VERSION
44
# The version of TexLogParser.
55
class TexLogParser
6-
VERSION = '1.1.0'
6+
VERSION = '1.1.1'
77
end

resources/message.schema.json

+68-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,71 @@
11
{
2-
"level": "info",
3-
"source_file": "/usr/local/texlive/2016/texmf-dist/tex/latex/base/fleqn.clo",
4-
"source_lines": {
5-
"from": 51,
6-
"to": 51
2+
"title": "(La)TeX Log",
3+
"description": "Content of a (La)TeX log as parsed by texlogparser.",
4+
"$schema": "http://json-schema.org/draft-06/schema#",
5+
6+
"definitions": {
7+
"lines": {
8+
"type": "object",
9+
"properties": {
10+
"from": {
11+
"type": "integer"
12+
},
13+
"to": {
14+
"type": "integer"
15+
}
16+
},
17+
"required": [
18+
"from",
19+
"to"
20+
]
21+
}
722
},
8-
"message": "LaTeX Info: Redefining \\[ on input line 51.",
9-
"log_lines": {
10-
"from": 15,
11-
"to": 15
12-
},
13-
"preformatted": true
23+
24+
"type": "array",
25+
"items": {
26+
"type": "object",
27+
"properties": {
28+
"level": {
29+
"type": "string",
30+
"enum": [
31+
"error",
32+
"warning",
33+
"info"
34+
]
35+
},
36+
"message": {
37+
"type": "string"
38+
},
39+
"source_file": {
40+
"oneOf": [
41+
{
42+
"type": "string"
43+
},
44+
{
45+
"type": "null"
46+
}
47+
]
48+
},
49+
"source_lines": {
50+
"oneOf": [
51+
{
52+
"$ref": "#/definitions/lines"
53+
},
54+
{
55+
"type": "null"
56+
}
57+
]
58+
},
59+
"log_lines": {
60+
"$ref": "#/definitions/lines"
61+
},
62+
"preformatted": {
63+
"type": "boolean"
64+
}
65+
},
66+
"required": [
67+
"level",
68+
"message"
69+
]
70+
}
1471
}

test/test_jsonlog.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
require 'minitest/autorun'
4+
require 'json-schema'
5+
require 'tex_log_parser'
6+
7+
# Tests whether {LogParser::Buffer} works correctly when reading from arrays.
8+
class JsonLogTests < Minitest::Test
9+
def initialize(param)
10+
super(param)
11+
12+
schema = "#{File.expand_path(__dir__)}/../resources/message.schema.json"
13+
@schema = File.read(schema)
14+
end
15+
16+
def test_schema_valid
17+
meta_schema = JSON::Validator.validator_for_uri('http://json-schema.org/draft-06/schema#').metaschema
18+
errors = JSON::Validator.fully_validate(meta_schema, @schema)
19+
errors.each { |e| puts e.to_s }
20+
assert(errors.empty?, 'We should have a valid schema.')
21+
end
22+
23+
def test_validity_against_schema
24+
Dir["#{File.expand_path(__dir__)}/texlogs/*.log"].each do |log|
25+
LogParser::Logger.debug "\n\nValidating #{log}"
26+
json = JSON.pretty_generate(TexLogParser.new(File.open(log, 'r')).parse)
27+
LogParser::Logger.debug json
28+
errors = JSON::Validator.fully_validate(@schema, json)
29+
errors.each { |e| puts e.to_s }
30+
assert(errors.empty?, "JSON output of parsed '#{log}' should be valid.")
31+
end
32+
end
33+
end

texlogparser.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
2020
s.files = Dir['lib/**/*.rb', 'bin/*', 'LICENSE', '*.md']
2121

2222
s.add_development_dependency 'github-markup', '~> 2.0'
23+
s.add_development_dependency 'json-schema', '~> 2.8'
2324
s.add_development_dependency 'minitest', '~> 5.10'
2425
s.add_development_dependency 'rake', '~> 12.3'
2526
s.add_development_dependency 'redcarpet', '~> 3.4'

0 commit comments

Comments
 (0)