Skip to content

Commit

Permalink
Syntax-check go with a standalone script
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Dec 1, 2013
1 parent 2ce5d14 commit 195495f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
13 changes: 3 additions & 10 deletions lib/language/go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,11 @@ def solution_dump(attributes)
end

def parsing?(code)
return true if code.empty?
TempDir.for('code.go' => code) do |dir|
code_path = dir.join('code.go')
build_result = nil
gofmt_result = nil
script_path = Rails.root.join('lib/language/go/syntax_check.rb')
code_path = dir.join('code.go')

FileUtils.cd(dir) do
build_result = `go build #{code_path} 2>&1`
gofmt_result = `gofmt -d #{code_path} 2>&1`
end

build_result.strip.empty? and gofmt_result.strip.empty?
system script_path.to_s, code_path.to_s
end
end

Expand Down
31 changes: 31 additions & 0 deletions lib/language/go/syntax_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /usr/bin/env ruby

require 'tmpdir'

code = ARGF.read

exit 0 if code.empty?

if code !~ /^\s*func\s+main\(\s*\)/
code += "\nfunc main() {\n}\n"
end

build_result = nil
gofmt_result = nil

Dir.mktmpdir do |dir|
FileUtils.cd(dir) do
File.open('code.go', 'w') do |f|
f.write(code)
end

build_result = `go build code.go 2>&1`
gofmt_result = `gofmt -d code.go 2>&1`
end
end

if build_result.strip.empty? and gofmt_result.strip.empty?
exit 0
else
exit 1
end
6 changes: 6 additions & 0 deletions spec/lib/language/parsing_go_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
CODE
end

it "returns true for valid code without a main function" do
Language::Go.should be_parsing <<CODE
package main
CODE
end

it "returns true for no code" do
Language::Go.should be_parsing ""
end
Expand Down

0 comments on commit 195495f

Please sign in to comment.