Repository: oasis-open/cti-stix-validator
Pull request #67 (2018-07-06 21:49:14)

Allow input from stdin


Patch:
diff --git a/docs/conf.py b/docs/conf.py
index 5a5c2b4..490b1d2 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -35,3 +35,9 @@ latex_documents = [
 man_pages = [
     (master_doc, project, 'stix2-validator Documentation', [author], 1),
 ]
+
+latex_documents = [
+    (master_doc, project, 'stix2-validator Documentation', author, project,
+     'Validator for STIX 2 normative requirements and best practices',
+     'Miscellaneous'),
+]
diff --git a/setup.cfg b/setup.cfg
index 3f08369..ed6a48a 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -7,8 +7,6 @@ tag = True
 
 [bumpversion:file:docs/conf.py]
 
-[metadata]
-license_file = LICENSE
-
 [bdist_wheel]
 universal = 1
+
diff --git a/stix2validator/scripts/stix2_validator.py b/stix2validator/scripts/stix2_validator.py
index a3d1631..63d6927 100644
--- a/stix2validator/scripts/stix2_validator.py
+++ b/stix2validator/scripts/stix2_validator.py
@@ -4,6 +4,7 @@
 """
 
 import logging
+import os
 import sys
 
 from stix2validator import (ValidationError, codes, output, parse_args,
@@ -16,6 +17,10 @@ def main():
     # Parse command line arguments
     options = parse_args(sys.argv[1:], is_script=True)
 
+    # Only print prompt if script is run on cmdline and no input is piped in
+    if options.files == sys.stdin and os.isatty(0):
+        print('Input STIX content, then press Ctrl+D: ')
+
     try:
         # Validate input documents
         results = run_validation(options)
diff --git a/stix2validator/util.py b/stix2validator/util.py
index 8a9ce11..98eb2bd 100644
--- a/stix2validator/util.py
+++ b/stix2validator/util.py
@@ -136,9 +136,10 @@ def parse_args(cmd_args, is_script=False):
         parser.add_argument(
             "files",
             metavar="FILES",
-            nargs="+",
+            nargs="*",
+            default=sys.stdin,
             help="A whitespace separated list of STIX files or directories of "
-                 "STIX files to validate."
+                 "STIX files to validate. If none given, stdin will be used."
         )
     parser.add_argument(
         "-r",
diff --git a/stix2validator/validator.py b/stix2validator/validator.py
index 270f12b..ba5df60 100644
--- a/stix2validator/validator.py
+++ b/stix2validator/validator.py
@@ -5,6 +5,7 @@ from collections import Iterable
 import io
 from itertools import chain
 import os
+import sys
 
 from jsonschema import Draft4Validator, RefResolver
 from jsonschema import exceptions as schema_exceptions
@@ -330,11 +331,16 @@ def run_validation(options):
             this validation run.
 
     """
-    # The JSON files to validate
+    if options.files == sys.stdin:
+        results = validate(options.files, options)
+        return [FileValidationResults(is_valid=results.is_valid,
+                                      filepath='stdin',
+                                      object_results=results)]
+
     try:
         files = get_json_files(options.files, options.recursive)
     except NoJSONFileFoundError as e:
-        output.error(e.message)
+        output.error(e)
 
     results = [validate_file(fn, options) for fn in files]
 


Test patch:
diff --git a/stix2validator/test/misc_tests.py b/stix2validator/test/misc_tests.py
new file mode 100644
index 0000000..9209652
--- /dev/null
+++ b/stix2validator/test/misc_tests.py
@@ -0,0 +1,31 @@
+import os
+import sys
+
+import pytest
+
+from . import ValidatorTest
+from .. import ValidationOptions, run_validation
+from .tool_tests import VALID_TOOL
+
+
+class MiscTestCases(ValidatorTest):
+
+    def test_run_validation(self):
+        inputfile = os.path.join(os.path.dirname(os.path.realpath(__file__)),
+                                 '..', 'schemas', 'examples',
+                                 'using-granular-markings.json')
+        options = ValidationOptions(files=[inputfile])
+        results = run_validation(options)
+        assert results[0].is_valid
+
+    def test_run_validation_nonexistent_file(self):
+        options = ValidationOptions(files='asdf.json')
+        with pytest.raises(SystemExit):
+            run_validation(options)
+
+
+def test_run_validation_stdin(monkeypatch):
+    monkeypatch.setattr(sys.stdin, 'read', lambda: VALID_TOOL)
+    options = ValidationOptions(files=sys.stdin)
+    results = run_validation(options)
+    assert results[0].is_valid