From 3de8edff0ae41f2a351a4a243c17643cfe27363f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Tue, 29 May 2018 13:34:03 +0200 Subject: [PATCH 1/5] Test for code block on markdown files for now it tests only syntax of bash code starts with '$' --- spec/code_block_spec.rb | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 spec/code_block_spec.rb diff --git a/spec/code_block_spec.rb b/spec/code_block_spec.rb new file mode 100644 index 0000000..d2a4ee1 --- /dev/null +++ b/spec/code_block_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' +require 'kramdown' +require 'ap' +require 'pp' +site = File.join(File.dirname(__FILE__), '..','content', '**', '*.md') +PAGES = Dir.glob(site) + +def extract_code(element,prev_element) + codes = [] + if element.type == :codespan \ + && !element.value.nil? \ + && !codes.include?(element.value) \ + && ((prev_element.children.size == 1) || (element.value[/^\n/])) #element value hasn't got any sibling or begin with '\n' + codes.push element.value + end + element.children.each do |child| + codes += extract_code(child, element) + end + codes +end + +PAGES.each do |page| + generated_page = Kramdown::Document.new(File.read(page)) + codes = extract_code(generated_page.root,generated_page) + + describe 'Code blocks on markdown' do + + + before :each do + codes.each do |code| + code.gsub! "\n",'' + end + end + it 'have good syntax' do + codes.each do |code| + if code[/^\$/] #code starts with '$' + expect(system('bash', '-n', '-c', code, '&>/dev/null')).to be_truthy,"Something wrong with: '#{code}'" + elsif code[/^\#/] #code starts with '#' + #ap code + else #code starts with something else + #ap code + end + end + end + end +end From d38693e6983c3bf3b173e83cce4f55da515db799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Wed, 30 May 2018 10:45:01 +0200 Subject: [PATCH 2/5] rename variable "site" to "content", move comment, edit codes inside method extract_code, add Array of results from testing bash syntax --- spec/code_block_spec.rb | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/spec/code_block_spec.rb b/spec/code_block_spec.rb index d2a4ee1..d956279 100644 --- a/spec/code_block_spec.rb +++ b/spec/code_block_spec.rb @@ -1,17 +1,18 @@ require 'spec_helper' require 'kramdown' require 'ap' -require 'pp' -site = File.join(File.dirname(__FILE__), '..','content', '**', '*.md') -PAGES = Dir.glob(site) + +content = File.join(File.dirname(__FILE__), '..','content', '**', '*.md') +PAGES = Dir.glob(content) def extract_code(element,prev_element) - codes = [] + codes = [] + #element value hasn't got any sibling or begin with '\n' if element.type == :codespan \ && !element.value.nil? \ && !codes.include?(element.value) \ - && ((prev_element.children.size == 1) || (element.value[/^\n/])) #element value hasn't got any sibling or begin with '\n' - codes.push element.value + && ((prev_element.children.size == 1) || (element.value[/^\n/])) + codes.push element.value.gsub "\n",'' end element.children.each do |child| codes += extract_code(child, element) @@ -22,25 +23,21 @@ def extract_code(element,prev_element) PAGES.each do |page| generated_page = Kramdown::Document.new(File.read(page)) codes = extract_code(generated_page.root,generated_page) - + results = [] + describe 'Code blocks on markdown' do - - before :each do - codes.each do |code| - code.gsub! "\n",'' - end - end it 'have good syntax' do codes.each do |code| if code[/^\$/] #code starts with '$' - expect(system('bash', '-n', '-c', code, '&>/dev/null')).to be_truthy,"Something wrong with: '#{code}'" + results.push([code,system('bash', '-n', '-c', code, "&>/dev/null")]) + expect(results[-1][1]).to be_truthy,"Something wrong with: '#{code}'" elsif code[/^\#/] #code starts with '#' #ap code else #code starts with something else #ap code end end - end + end end end From f4cbfb54ab53db64c86a69b0f961da9e30d7c288 Mon Sep 17 00:00:00 2001 From: Pavel Valena Date: Thu, 31 May 2018 14:17:56 +0200 Subject: [PATCH 3/5] WIP: code_block_spec.rb refactoring and add comments. --- spec/code_block_spec.rb | 60 ++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/spec/code_block_spec.rb b/spec/code_block_spec.rb index d956279..6c335c6 100644 --- a/spec/code_block_spec.rb +++ b/spec/code_block_spec.rb @@ -5,39 +5,57 @@ content = File.join(File.dirname(__FILE__), '..','content', '**', '*.md') PAGES = Dir.glob(content) -def extract_code(element,prev_element) - codes = [] - #element value hasn't got any sibling or begin with '\n' - if element.type == :codespan \ - && !element.value.nil? \ - && !codes.include?(element.value) \ - && ((prev_element.children.size == 1) || (element.value[/^\n/])) - codes.push element.value.gsub "\n",'' - end - element.children.each do |child| - codes += extract_code(child, element) +def extract_code(parent) + codespans = [] + parent.children.each do |child| + + # Checks ... parent value hasn't got any sibling or begin with '\n' + #<<< TODO: ^^^ Why? + if child.type == :codespan \ + && !child.value.nil? \ #<<< remove?, investigate + && !codespans.include?(child.value) \ #<<< remove, keep context + && ((parent.children.size == 1) || (child.value[/^\n/])) #<<< use start_with? + + codespans.push child.value.gsub "\n",' ' #<<< .split and add to codespans + #<<< add .trim - white space in the beginning and end + end + + codespans += extract_code(child) end - codes + codespans end PAGES.each do |page| - generated_page = Kramdown::Document.new(File.read(page)) - codes = extract_code(generated_page.root,generated_page) - results = [] + generated_page = Kramdown::Document.new(File.read(page)) #<<< MD + codespans = extract_code(generated_page.root) #<<< lines of code, rename? + results = [] #<<< to Hash: {codespan => result} - describe 'Code blocks on markdown' do + describe 'Command in Markdown file' do - it 'have good syntax' do - codes.each do |code| + it 'has a good syntax' do #<<< passes syntax check + codespans.each do |code| if code[/^\$/] #code starts with '$' - results.push([code,system('bash', '-n', '-c', code, "&>/dev/null")]) + results.push([code,system('bash', '-n', '-c', code, "&>/dev/null")]) #<<< do not execute here expect(results[-1][1]).to be_truthy,"Something wrong with: '#{code}'" - elsif code[/^\#/] #code starts with '#' + elsif code[/^\#/] #<<< code.starts_with? ?# #ap code else #code starts with something else #ap code end end - end + end + + it 'does not begin with #' do + skip + end + + it 'starts with $' do + skip + end + + it 'runs dnf with sudo' do + skip + end + end end From 92643b11a5db40351af72e094970099a8fb0c7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Fri, 1 Jun 2018 10:23:44 +0200 Subject: [PATCH 4/5] Script change Add comment, remove unnecessary parts of condition, deleting '\n' from code replace with an array of codelines without '\n' on start/end and whitespaces, rename variables, replace 'results' Array with Hash, change code structure to be easy readable --- spec/code_block_spec.rb | 60 ++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/spec/code_block_spec.rb b/spec/code_block_spec.rb index 6c335c6..6547856 100644 --- a/spec/code_block_spec.rb +++ b/spec/code_block_spec.rb @@ -8,54 +8,40 @@ def extract_code(parent) codespans = [] parent.children.each do |child| - - # Checks ... parent value hasn't got any sibling or begin with '\n' - #<<< TODO: ^^^ Why? + # Checks if child.value hasn't got any sibling or begin with '\n', because only code is needed if child.type == :codespan \ - && !child.value.nil? \ #<<< remove?, investigate - && !codespans.include?(child.value) \ #<<< remove, keep context - && ((parent.children.size == 1) || (child.value[/^\n/])) #<<< use start_with? - - codespans.push child.value.gsub "\n",' ' #<<< .split and add to codespans - #<<< add .trim - white space in the beginning and end + && ((parent.children.size == 1) || (child.value.start_with? ?\n)) + codespans += child.value.strip().split("\n").map! { |x| x.strip()} end - codespans += extract_code(child) end codespans end PAGES.each do |page| - generated_page = Kramdown::Document.new(File.read(page)) #<<< MD - codespans = extract_code(generated_page.root) #<<< lines of code, rename? - results = [] #<<< to Hash: {codespan => result} - + markdown_document = Kramdown::Document.new(File.read(page)) + code_lines = extract_code(markdown_document.root) + results = {} + code_lines.each do |code| + results[code] = system("bash", "-n", "-c", code, :err => File::NULL) + end describe 'Command in Markdown file' do + results.keys.each do |code| + it 'has a good syntax' do + expect(results[code]).to be_truthy,"Something wrong with: '#{code}'" + end + next + it 'does not begin with #' do + skip + end - it 'has a good syntax' do #<<< passes syntax check - codespans.each do |code| - if code[/^\$/] #code starts with '$' - results.push([code,system('bash', '-n', '-c', code, "&>/dev/null")]) #<<< do not execute here - expect(results[-1][1]).to be_truthy,"Something wrong with: '#{code}'" - elsif code[/^\#/] #<<< code.starts_with? ?# - #ap code - else #code starts with something else - #ap code - end + it 'starts with $' do + skip + end + + it 'runs dnf with sudo' do + skip end end - - it 'does not begin with #' do - skip - end - - it 'starts with $' do - skip - end - - it 'runs dnf with sudo' do - skip - end - end end From ceadc093880cc087f66f499a8357454833b69bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=C3=ADn=20Mrkvica?= Date: Fri, 1 Jun 2018 14:32:52 +0200 Subject: [PATCH 5/5] rename variables, change extract method and extend error message rename PAGES to MD_FILES and codespans to code_lines, change extract method, add file path into decsribe error message, replace 'each' with 'inject' --- spec/code_block_spec.rb | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/spec/code_block_spec.rb b/spec/code_block_spec.rb index 6547856..a678fbc 100644 --- a/spec/code_block_spec.rb +++ b/spec/code_block_spec.rb @@ -1,34 +1,47 @@ require 'spec_helper' require 'kramdown' -require 'ap' content = File.join(File.dirname(__FILE__), '..','content', '**', '*.md') -PAGES = Dir.glob(content) +MD_FILES = Dir.glob(content) def extract_code(parent) - codespans = [] + code_lines = [] + parent.children.each do |child| # Checks if child.value hasn't got any sibling or begin with '\n', because only code is needed if child.type == :codespan \ && ((parent.children.size == 1) || (child.value.start_with? ?\n)) - codespans += child.value.strip().split("\n").map! { |x| x.strip()} + code_lines += child.value + .split(?\n) + .map(&:strip) + .inject([]) do |res, cu| + if !res.last.nil? && res.last.end_with?("\\") + res.last[0..-2].concat cu + else + res.push cu + end + res + end end - codespans += extract_code(child) + + code_lines += extract_code child end - codespans + code_lines.reject(&:empty?) end -PAGES.each do |page| - markdown_document = Kramdown::Document.new(File.read(page)) +MD_FILES.each do |file| + markdown_document = Kramdown::Document.new(File.read(file)) code_lines = extract_code(markdown_document.root) - results = {} - code_lines.each do |code| - results[code] = system("bash", "-n", "-c", code, :err => File::NULL) + + results = code_lines.inject({}) do |r, code| + r[code] = system("bash", "-n", "-c", code, :err => File::NULL) + r end - describe 'Command in Markdown file' do - results.keys.each do |code| + + describe "Command in Markdown file '#{file.gsub(/[^_]+\/\.\.\/(content\/.*)/, '\\1')}'" do + results.each do |code, result| it 'has a good syntax' do - expect(results[code]).to be_truthy,"Something wrong with: '#{code}'" + expect(result).to be_truthy,"Something wrong with: '#{code}'" end next it 'does not begin with #' do