Skip to content

Commit

Permalink
[gpkg] Error if file does not exists.
Browse files Browse the repository at this point in the history
Before gpkg would create an empty database file if it does not exists,
now we check to see if the file exists beforing attempting to open it
with sqlite3. If it does not exists we error out.
  • Loading branch information
gdey committed May 15, 2018
1 parent f8ff6e1 commit 603ca74
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions provider/gpkg/gpkg_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"errors"
"fmt"
"os"
"strings"

_ "github.com/mattn/go-sqlite3"
Expand All @@ -32,6 +33,10 @@ func NewTileProvider(config map[string]interface{}) (provider.Tiler, error) {
return nil, ErrInvalidFilePath{filepath}
}

if _, err := os.Stat(filepath); os.IsNotExist(err) {
return nil, ErrInvalidFilePath{filepath}
}

db, err := sql.Open("sqlite3", filepath)
if err != nil {
return nil, err
Expand Down
43 changes: 43 additions & 0 deletions provider/gpkg/gpkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"context"
"errors"
"fmt"
"os"
"reflect"
"testing"

"github.com/go-spatial/tegola"
Expand Down Expand Up @@ -419,3 +421,44 @@ func TestConfigs(t *testing.T) {
})
}
}

// This is just to test that if we open a non-existant file.
func TestOpenNonExistantFile(t *testing.T) {

type tcase struct {
config map[string]interface{}
err error
}
const (
NONEXISTANTFILE = "testdata/nonexistant.gpkg"
)

os.Remove(NONEXISTANTFILE)

tests := map[string]tcase{
"empty": tcase{
config: map[string]interface{}{
gpkg.ConfigKeyFilePath: "",
},
err: gpkg.ErrInvalidFilePath{FilePath: ""},
},
"nonexistance": tcase{
// should not exists
config: map[string]interface{}{
gpkg.ConfigKeyFilePath: NONEXISTANTFILE,
},
err: gpkg.ErrInvalidFilePath{FilePath: NONEXISTANTFILE},
},
}

for k, tc := range tests {
tc := tc
t.Run(k, func(t *testing.T) {
_, err := gpkg.NewTileProvider(tc.config)
if reflect.TypeOf(err) != reflect.TypeOf(tc.err) {
t.Errorf("expected error, expected %v got %v", tc.err, err)
}
})
}

}

0 comments on commit 603ca74

Please sign in to comment.