Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for --test-only DIR #230

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Options:
-l LEVEL, --log-level LEVEL Logging level, 0-3: debug, info, warning, error.
-s, --skip-external Skip external link checks, may shorten execution
time considerably.
-t, --test-only DIR Site subdirectory to test, relative to the site base path
-v, --version Show version and build time.
```

Expand Down Expand Up @@ -135,6 +136,7 @@ htmltest uses a YAML configuration file. Put `.htmltest.yml` in the same directo
| `DirectoryIndex` | The file to look for when linking to a directory. | `index.html` |
| `FilePath` | Single file to test within `DirectoryPath`, omit to test all. | |
| `FileExtension` | Extension of your HTML documents, includes the dot. If `FilePath` is set we use the extension from that. | `.html` |
| `TestOnlyDir` | Single directory to test within `DirectoryPath`, omit to test all. | |
| `CheckDoctype` | Enables checking the document type declaration. | `true` |
| `CheckAnchors` | Enables checking `<a…` tags. | `true` |
| `CheckLinks` | Enables checking `<link…` tags. | `true` |
Expand Down
6 changes: 6 additions & 0 deletions htmldoc/document_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"regexp"
"strings"

"github.com/wjdp/htmltest/output"
)
Expand All @@ -15,6 +16,7 @@ import (
type DocumentStore struct {
BasePath string // Path, relative to cwd, the site is located in
IgnorePatterns []interface{} // Regexes of directories to ignore
TestOnlyDir string // Clean non-nil path of directory to test relative to `BasePath`
Documents []*Document // All of the documents, used to iterate over
DocumentPathMap map[string]*Document // Maps slash separated paths to documents
DocumentExtension string // File extension to look for
Expand Down Expand Up @@ -51,6 +53,10 @@ func (dS *DocumentStore) isDirIgnored(dir string) bool {
return true
}
}
if dS.TestOnlyDir != "" {
matchesTestOnlyDir := strings.HasPrefix(dir, dS.TestOnlyDir)
return !matchesTestOnlyDir
}
return false
}

Expand Down
22 changes: 22 additions & 0 deletions htmldoc/document_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ func TestDocumentStoreIgnorePatterns(t *testing.T) {
assert.Equals(t, "document count", len(dS.Documents), 6)
}

func TestDocumentStoreTestOnlyDir(t *testing.T) {
dS := NewDocumentStore()
dS.BasePath = "fixtures/documents"
dS.DocumentExtension = ".html" // Ignores .htm
dS.DirectoryIndex = "index.html"
dS.TestOnlyDir = "dir1"
dS.Discover()
// TestOnlyDir does not affect stored document count
assert.Equals(t, "document count", len(dS.Documents), 6)
// TODO: uncomment the following if/once https://github.com/wjdp/htmltest/pull/229 is merged
// assert.Equals(t, "ignored document count", dS.IgnoredDocCount(), 4)
ignoredFile := "dir2/index.html"
assert.IsTrue(t, ignoredFile+" is ignored", dS.DocumentPathMap[ignoredFile].IgnoreTest)

testFiles := [2]string{"dir1/index.html", "dir1/dir11/index.html"}
for _, keptFile := range testFiles {
f, exists := dS.DocumentPathMap[keptFile]
assert.IsTrue(t, keptFile+" exists", exists)
assert.IsFalse(t, keptFile+" is not ignored", f.IgnoreTest)
}
}

func TestDocumentStoreDocumentExists(t *testing.T) {
// documentstore knows if documents exist or not
dS := NewDocumentStore()
Expand Down
1 change: 1 addition & 0 deletions htmltest/htmltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func Test(optsUser map[string]interface{}) (*HTMLTest, error) {
hT.documentStore.DirectoryIndex = hT.opts.DirectoryIndex
hT.documentStore.IgnorePatterns = hT.opts.IgnoreDirs
hT.documentStore.IgnoreTagAttribute = hT.opts.IgnoreTagAttribute
hT.documentStore.TestOnlyDir = hT.opts.TestOnlyDir
// Discover documents
hT.documentStore.Discover()

Expand Down
10 changes: 10 additions & 0 deletions htmltest/htmltest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ func TestCountErrors(t *testing.T) {
assert.Equals(t, "CountErrors", hT.CountErrors(), 2)
}

func TestCountFolderDocuments(t *testing.T) {
hT := tTestDirectoryOpts("fixtures/documents/folder-ok", map[string]interface{}{
"TestOnlyDir": "a",
})
assert.Equals(t, "CountDocuments", hT.CountDocuments(), 3)
// TODO: uncomment the following if/once https://github.com/wjdp/htmltest/pull/229 is merged
// assert.Equals(t, "CountTestedDocuments", hT.CountTestedDocuments(), 1)
assert.Equals(t, "CountErrors", hT.CountErrors(), 0)
}

func TestFileExtensionDefault(t *testing.T) {
// Non .html files are ignored
hT := tTestDirectory("fixtures/documents/folder-htm")
Expand Down
2 changes: 2 additions & 0 deletions htmltest/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Options struct {
DirectoryIndex string
FilePath string
FileExtension string
TestOnlyDir string

CheckDoctype bool
CheckAnchors bool
Expand Down Expand Up @@ -86,6 +87,7 @@ func DefaultOptions() map[string]interface{} {
return map[string]interface{}{
"DirectoryIndex": "index.html",
"FileExtension": ".html",
"TestOnlyDir": "",

"CheckDoctype": true,
"CheckAnchors": true,
Expand Down
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Options:
-l LEVEL, --log-level LEVEL Logging level, 0-3: debug, info, warning, error.
-s, --skip-external Skip external link checks, may shorten execution
time considerably.
-t, --test-only DIR Site subdirectory to test, relative to the site base path
-v, --version Show version and build time.
`
versionText := "htmltest " + version
Expand Down Expand Up @@ -139,6 +140,36 @@ func augmentWithCLIArgs(options optsMap, arguments map[string]interface{}) {

}

if arguments["--test-only"] != nil {
var testOnlyDir = path.Clean(arguments["--test-only"].(string))
options["TestOnlyDir"] = testOnlyDir

var directoryPath string = ""
if options["DirectoryPath"] != nil {
directoryPath = options["DirectoryPath"].(string)
}
var testOnlyDirFullPath = testOnlyDir

if directoryPath != "" && !strings.HasPrefix(testOnlyDir, "/") && !strings.HasPrefix(testOnlyDir, directoryPath) {
testOnlyDirFullPath = path.Join(directoryPath, testOnlyDir)
}

f, err := os.Open(path.Clean(testOnlyDirFullPath))
if os.IsNotExist(err) {
output.AbortWith("Cannot access --test-only path '" + testOnlyDirFullPath + "', no such directory.")
}
output.CheckErrorGeneric(err)
defer f.Close()

fi, err := f.Stat()
output.CheckErrorPanic(err)

if !fi.IsDir() {
output.AbortWith("Argument to --test-only must be directory: '" + testOnlyDirFullPath + "'")
}

}

if arguments["--log-level"] != nil {
if ll, err := strconv.Atoi(arguments["--log-level"].(string)); err == nil && ll >= 0 {
options["LogLevel"] = ll
Expand Down
Loading