Exemple #1
0
def test_detect_encoding():
  # Quick detection should yield utf-8 for the petstore file.
  assert fs.detect_encoding('tests/specs/petstore.yaml') == 'utf-8'

  # Really, it should be detected as ISO-8859-1 as a superset of ASCII
  assert fs.detect_encoding('tests/specs/petstore.yaml',
                            default_to_utf8 = False) == 'iso-8859-1'

  # Deep inspection should yield UTF-8 again.
  assert fs.detect_encoding('tests/specs/petstore.yaml',
                            default_to_utf8 = False,
                            read_all = True) == 'utf-8'

  # The UTF-8 file with BOM should be detected properly
  assert fs.detect_encoding('tests/specs/utf8bom.yaml') == 'utf-8-sig'
Exemple #2
0
def test_validate_output(runner):
    from prance import cli

    import os, os.path

    curdir = os.getcwd()

    outnames = ["foo.json", "foo.yaml"]
    for outname in outnames:
        with runner.isolated_filesystem():
            result = runner.invoke(
                cli.validate,
                [
                    "-o", outname,
                    os.path.join(curdir, "tests/specs/petstore.yaml")
                ],
            )
            assert result.exit_code == 0

            # There also must be a 'foo' file now.
            files = [f for f in os.listdir(".") if os.path.isfile(f)]
            assert outname in files

            # Ensure a UTF-8 file encoding.
            from prance.util import fs

            assert "utf-8" in fs.detect_encoding(outname,
                                                 default_to_utf8=False,
                                                 read_all=True)

            # Now do a full encoding detection, too

            # The 'foo' file must be a valid swagger spec.
            result = runner.invoke(cli.validate, [outname])
            assert result.exit_code == 0
Exemple #3
0
def test_detect_encoding():
    # Quick detection should yield utf-8 for the petstore file.
    assert fs.detect_encoding("tests/specs/petstore.yaml") == "utf-8"

    # Without defaulting to utf-8, it should also work.
    assert (fs.detect_encoding("tests/specs/petstore.yaml",
                               default_to_utf8=False) == "utf-8")

    # Deep inspection should not change anything because the file size is
    # too small.
    assert (fs.detect_encoding("tests/specs/petstore.yaml",
                               default_to_utf8=False,
                               read_all=True) == "utf-8")

    # The UTF-8 file with BOM should be detected properly
    assert fs.detect_encoding("tests/specs/utf8bom.yaml") == "utf-8-sig"
Exemple #4
0
def test_convert_output(runner):
  from prance import cli

  import os, os.path
  curdir = os.getcwd()

  outnames = ['foo.json', 'foo.yaml']
  for outname in outnames:
    with runner.isolated_filesystem():
      result = runner.invoke(cli.convert,
          [os.path.join(curdir, 'tests/specs/petstore.yaml'), outname])
      assert result.exit_code == 0

      # There also must be a 'foo' file now.
      files = [f for f in os.listdir('.') if os.path.isfile(f)]
      assert outname in files

      # Ensure a UTF-8 file encoding.
      from prance.util import fs
      assert 'utf-8' in fs.detect_encoding(outname, default_to_utf8 = False,
                                           read_all = True)

      # Now do a full encoding detection, too
      contents = fs.read_file(outname)
      assert 'openapi' in contents
      assert '3.' in contents
Exemple #5
0
def test_issue_51_detect_encoding():
    # This should be UTF-8, but in issue 51 it was reported that it comes back
    # as iso-8859-2

    # Detection should be ok if the entire file is read
    assert (fs.detect_encoding("tests/specs/issue_51/openapi-part.yaml",
                               read_all=True) == "utf-8")

    # After the heuristic change, it reads the whole file anyway.
    assert fs.detect_encoding(
        "tests/specs/issue_51/openapi-part.yaml") == "utf-8"

    # Specifically re-encoded as iso-8859-2 should fail - but not as
    # a call to the detect_encoding() function. Instead, we can only return
    # a badly detected encoding. Chardet sends iso-8859-1 here.
    assert (fs.detect_encoding(
        "tests/specs/issue_51/openapi-part-iso-8859-2.yaml") == "iso-8859-1")
Exemple #6
0
def test_write_file_bom(tmpdir):
    with sandbox.sandbox(tmpdir):
        test_text = "söme täxt"
        fs.write_file("test.out", test_text, "utf-8-sig")

        # File must have been written
        files = [f for f in os.listdir(".") if os.path.isfile(f)]
        assert "test.out" in files

        # Encoding must match the one we've given
        encoding = fs.detect_encoding("test.out")
        assert encoding == "utf-8-sig"

        # File contents must work
        contents = fs.read_file("test.out")
        assert test_text == contents
Exemple #7
0
def test_write_file_bom(tmpdir):
    with sandbox.sandbox(tmpdir):
        test_text = u'söme täxt'
        fs.write_file('test.out', test_text, 'utf-8-sig')

        # File must have been written
        files = [f for f in os.listdir('.') if os.path.isfile(f)]
        assert 'test.out' in files

        # Encoding must match the one we've given
        encoding = fs.detect_encoding('test.out')
        assert encoding == 'utf-8-sig'

        # File contents must work
        contents = fs.read_file('test.out')
        assert test_text == contents
Exemple #8
0
def test_write_file_bom():
    # What we're doing here has really nothing to do with click's CliRunner,
    # but since we have it, we might as well use its sandboxing feature.
    from click.testing import CliRunner
    runner = CliRunner()
    with runner.isolated_filesystem():
        test_text = u'söme täxt'
        fs.write_file('test.out', test_text, 'utf-8-sig')

        # File must have been written
        files = [f for f in os.listdir('.') if os.path.isfile(f)]
        assert 'test.out' in files

        # Encoding must match the one we've given
        encoding = fs.detect_encoding('test.out')
        assert encoding == 'utf-8-sig'

        # File contents must work
        contents = fs.read_file('test.out')
        assert test_text == contents