Skip to content

Commit

Permalink
Add tools/src format check
Browse files Browse the repository at this point in the history
Adds a presubmit check to //tools/src to enforce that the Go code is
properly formatted.

Bug: chromium:343519239
Change-Id: I84b7c64486971c5194a4fbfc5d889c24d5aef0c0
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/190564
Reviewed-by: Austin Eng <[email protected]>
Commit-Queue: Brian Sheedy <[email protected]>
Auto-Submit: Brian Sheedy <[email protected]>
Commit-Queue: Austin Eng <[email protected]>
  • Loading branch information
bsheedy-work authored and Dawn LUCI CQ committed May 30, 2024
1 parent 80bb115 commit 7474d83
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
39 changes: 36 additions & 3 deletions go_presubmit_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,56 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


def go_bin_path(input_api):
return input_api.os_path.join(input_api.change.RepositoryRoot(), 'tools',
'golang', 'bin')


def go_path(input_api):
go = input_api.os_path.join(input_api.change.RepositoryRoot(), "tools",
"golang", "bin", "go")
go = input_api.os_path.join(go_bin_path(input_api), 'go')
if input_api.is_windows:
go += '.exe'

return go


def gofmt_path(input_api):
gofmt = input_api.os_path.join(go_bin_path(input_api), 'gofmt')
if input_api.is_windows:
go += '.exe'
return gofmt


def RunGoTests(input_api, output_api):
results = []
try:
input_api.subprocess.check_call_out(
[go_path(input_api), "test", "./..."],
[go_path(input_api), 'test', './...'],
stdout=input_api.subprocess.PIPE,
stderr=input_api.subprocess.PIPE,
cwd=input_api.PresubmitLocalPath())
except input_api.subprocess.CalledProcessError as e:
results.append(output_api.PresubmitError('%s' % (e, )))
return results


def EnforceGoFormatting(path, input_api, output_api):
errors = []
try:
stdout, _ = input_api.subprocess.check_call_out(
[gofmt_path(input_api), '-l', path],
stdout=input_api.subprocess.PIPE,
stderr=input_api.subprocess.STDOUT,
cwd=input_api.PresubmitLocalPath(),
text=True)
stdout = stdout.strip()
if stdout:
full_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
path)
errors.append(
output_api.PresubmitError(
'Go code in %s is not formatted, please run "gofmt -w %s"'
% (path, full_path)))
except input_api.subprocess.CalledProcessError as e:
errors.append(output_api.PresubmitError('EnforceGoFormatting: %s' % e))
return errors
8 changes: 6 additions & 2 deletions tools/PRESUBMIT.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
def _DoCommonChecks(input_api, output_api):
sys.path += [input_api.change.RepositoryRoot()]

from go_presubmit_support import RunGoTests
import go_presubmit_support

return RunGoTests(input_api, output_api)
results = go_presubmit_support.RunGoTests(input_api, output_api)
results.extend(
go_presubmit_support.EnforceGoFormatting('src', input_api, output_api))

return results


CheckChangeOnUpload = _DoCommonChecks
Expand Down

0 comments on commit 7474d83

Please sign in to comment.