How to use the prance.util.fs.detect_encoding function in prance

To help you get started, we’ve selected a few prance examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jfinkhaeuser / prance / tests / test_util_fs.py View on Github external
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'
github jfinkhaeuser / prance / tests / test_util_fs.py View on Github external
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
github jfinkhaeuser / prance / tests / test_util_fs.py View on Github external
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'
github jfinkhaeuser / prance / tests / test_util_fs.py View on Github external
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'
github jfinkhaeuser / prance / tests / test_util_fs.py View on Github external
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'
github jfinkhaeuser / prance / tests / test_util_fs.py View on Github external
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'
github jfinkhaeuser / prance / tests / test_util_fs.py View on Github external
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'
github jfinkhaeuser / prance / tests / test_cli.py View on Github external
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
github jfinkhaeuser / prance / tests / test_cli.py View on Github external
curdir = os.getcwd()

  outnames = ['foo.json', 'foo.yaml']
  for outname in outnames:
    with runner.isolated_filesystem():
      result = runner.invoke(cli.compile,
          [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

      # The 'foo' file must be a valid swagger spec.
      result = runner.invoke(cli.validate, [outname])
      assert result.exit_code == 0