Skip to content

Commit

Permalink
Minor improvements to skeema lint, testing
Browse files Browse the repository at this point in the history
`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 0fa2195
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
15 changes: 10 additions & 5 deletions cmd_lint.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"strings"


log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/skeema/mybase" "github.com/skeema/mybase"
Expand Down Expand Up @@ -59,15 +60,19 @@ func LintHandler(cfg *mybase.Config) error {


log.Infof("Linting %s", t.Dir) log.Infof("Linting %s", t.Dir)


for _, sf := range t.SQLFileErrors {
log.Error(sf.Error)
sqlErrCount++
}

ignoreTable, err := t.Dir.Config.GetRegexp("ignore-table") ignoreTable, err := t.Dir.Config.GetRegexp("ignore-table")
if err != nil { if err != nil {
return err 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 { for _, table := range t.SchemaFromDir.Tables {
if ignoreTable != nil && ignoreTable.MatchString(table.Name) { if ignoreTable != nil && ignoreTable.MatchString(table.Name) {
log.Warnf("Skipping table %s because ignore-table='%s'", table.Name, ignoreTable) log.Warnf("Skipping table %s because ignore-table='%s'", table.Name, ignoreTable)
Expand Down
16 changes: 8 additions & 8 deletions skeema_cmd_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -245,20 +245,19 @@ func (s *SkeemaIntegrationSuite) TestLintHandler(t *testing.T) {
// Manually restore the file with invalid SQL; the files should now verify, // 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 // confirming that the fatal error did not prevent the other files from being
// reformatted; re-linting should yield no changes. // reformatted; re-linting should yield no changes.
sqlFiles[0].Contents = strings.Replace(sqlFiles[0].Contents, "DEFALUT", "DEFAULT", 1) writeFile(t, sqlFiles[0].Path(), 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)
}
s.verifyFiles(t, cfg, "../golden/init") s.verifyFiles(t, cfg, "../golden/init")
s.handleCommand(t, CodeSuccess, ".", "skeema lint") s.handleCommand(t, CodeSuccess, ".", "skeema lint")


// Files with valid SQL, but not CREATE TABLE statements, should also trigger // Files with valid SQL, but not CREATE TABLE statements, should also trigger
// CodeFatalError. // CodeFatalError.
sqlFiles[0].Contents = "INSERT INTO foo (col1, col2) VALUES (123, 456)" writeFile(t, sqlFiles[0].Path(), "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)
}
s.handleCommand(t, CodeFatalError, ".", "skeema lint") 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) { func (s *SkeemaIntegrationSuite) TestDiffHandler(t *testing.T) {
Expand Down Expand Up @@ -498,6 +497,7 @@ func (s *SkeemaIntegrationSuite) TestIgnoreOptions(t *testing.T) {
contents := readFile(t, "mydb/analytics/_trending.sql") contents := readFile(t, "mydb/analytics/_trending.sql")
newContents := strings.Replace(contents, "`", "", -1) newContents := strings.Replace(contents, "`", "", -1)
writeFile(t, "mydb/analytics/_trending.sql", newContents) 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)") writeFile(t, "mydb/archives/bar.sql", "CREATE TABLE bar (this is not valid SQL whatever)")
s.handleCommand(t, CodeSuccess, ".", "skeema lint") s.handleCommand(t, CodeSuccess, ".", "skeema lint")
if readFile(t, "mydb/analytics/_trending.sql") != newContents { if readFile(t, "mydb/analytics/_trending.sql") != newContents {
Expand Down
8 changes: 8 additions & 0 deletions skeema_test.go
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
log "github.com/sirupsen/logrus"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
Expand Down Expand Up @@ -132,12 +133,19 @@ func (s *SkeemaIntegrationSuite) handleCommand(t *testing.T, expectedExitCode in
err := cfg.HandleCommand() err := cfg.HandleCommand()
if actualExitCode := ExitCode(err); actualExitCode != expectedExitCode { 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) 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 pwd != "" && pwd != "." {
if err := os.Chdir(s.workspace()); err != nil { if err := os.Chdir(s.workspace()); err != nil {
t.Fatalf("Unable to cd to %s: %s", s.workspace(), err) t.Fatalf("Unable to cd to %s: %s", s.workspace(), err)
} }
} }
fmt.Fprintf(os.Stderr, "\n")
return cfg return cfg
} }


Expand Down

0 comments on commit 0fa2195

Please sign in to comment.