Permalink
Browse files

Minor improvements to `skeema lint`, testing

`skeema lint` now ignores SQL errors in files with filenames that match value
of --ignore-table.

Added test to confirm `skeema lint` fatals on *.sql files with filename that
doesn't actually match the corresponding table name.

Command test output now includes log for exit code and message.
  • Loading branch information...
evanelias committed Jun 7, 2018
1 parent 8d463b5 commit 0fa21959e0823c007494bc39cea3eebf90fa12fa
Showing with 26 additions and 13 deletions.
  1. +10 −5 cmd_lint.go
  2. +8 −8 skeema_cmd_test.go
  3. +8 −0 skeema_test.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/skeema/mybase"
@@ -59,15 +60,19 @@ func LintHandler(cfg *mybase.Config) error {
log.Infof("Linting %s", t.Dir)
for _, sf := range t.SQLFileErrors {
log.Error(sf.Error)
sqlErrCount++
}
ignoreTable, err := t.Dir.Config.GetRegexp("ignore-table")
if err != nil {
return err
}
for _, sf := range t.SQLFileErrors {
assumedTableName := strings.TrimSuffix(sf.FileName, ".sql")
if ignoreTable == nil || !ignoreTable.MatchString(assumedTableName) {
log.Error(sf.Error)
sqlErrCount++
}
}
for _, table := range t.SchemaFromDir.Tables {
if ignoreTable != nil && ignoreTable.MatchString(table.Name) {
log.Warnf("Skipping table %s because ignore-table='%s'", table.Name, ignoreTable)
@@ -245,20 +245,19 @@ func (s *SkeemaIntegrationSuite) TestLintHandler(t *testing.T) {
// Manually restore the file with invalid SQL; the files should now verify,
// confirming that the fatal error did not prevent the other files from being
// reformatted; re-linting should yield no changes.
sqlFiles[0].Contents = strings.Replace(sqlFiles[0].Contents, "DEFALUT", "DEFAULT", 1)
if _, err := sqlFiles[0].Write(); err != nil {
t.Fatalf("Unable to rewrite %s: %s", sqlFiles[0].Path(), err)
}
writeFile(t, sqlFiles[0].Path(), strings.Replace(sqlFiles[0].Contents, "DEFALUT", "DEFAULT", 1))
s.verifyFiles(t, cfg, "../golden/init")
s.handleCommand(t, CodeSuccess, ".", "skeema lint")
// Files with valid SQL, but not CREATE TABLE statements, should also trigger
// CodeFatalError.
sqlFiles[0].Contents = "INSERT INTO foo (col1, col2) VALUES (123, 456)"
if _, err := sqlFiles[0].Write(); err != nil {
t.Fatalf("Unable to rewrite %s: %s", sqlFiles[0].Path(), err)
}
writeFile(t, sqlFiles[0].Path(), "INSERT INTO foo (col1, col2) VALUES (123, 456)")
s.handleCommand(t, CodeFatalError, ".", "skeema lint")
// Files with wrong table name should yield a fatal error, by virtue of
// SQLFile.Read() failing
writeFile(t, sqlFiles[0].Path(), "CREATE TABLE whatever (id int)")
s.handleCommand(t, CodeFatalError, ".", "skeema lint --debug")
}
func (s *SkeemaIntegrationSuite) TestDiffHandler(t *testing.T) {
@@ -498,6 +497,7 @@ func (s *SkeemaIntegrationSuite) TestIgnoreOptions(t *testing.T) {
contents := readFile(t, "mydb/analytics/_trending.sql")
newContents := strings.Replace(contents, "`", "", -1)
writeFile(t, "mydb/analytics/_trending.sql", newContents)
writeFile(t, "mydb/analytics/_hmm.sql", "lolololol no valid sql here")
writeFile(t, "mydb/archives/bar.sql", "CREATE TABLE bar (this is not valid SQL whatever)")
s.handleCommand(t, CodeSuccess, ".", "skeema lint")
if readFile(t, "mydb/analytics/_trending.sql") != newContents {
@@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
@@ -132,12 +133,19 @@ func (s *SkeemaIntegrationSuite) handleCommand(t *testing.T, expectedExitCode in
err := cfg.HandleCommand()
if actualExitCode := ExitCode(err); actualExitCode != expectedExitCode {
t.Errorf("Unexpected exit code from `%s`: Expected code=%d, found code=%d, message=%s", fullCommandLine, expectedExitCode, actualExitCode, err)
} else if actualExitCode == 0 {
log.Debug("Exit code 0 (SUCCESS)")
} else if actualExitCode >= CodeFatalError {
log.Error(err.Error())
} else {
log.Warn(err.Error())
}
if pwd != "" && pwd != "." {
if err := os.Chdir(s.workspace()); err != nil {
t.Fatalf("Unable to cd to %s: %s", s.workspace(), err)
}
}
fmt.Fprintf(os.Stderr, "\n")
return cfg
}

0 comments on commit 0fa2195

Please sign in to comment.