<commit_before># coding: utf-8
"""Test the /files/ handler."""

import io
import os
from unicodedata import normalize

pjoin = os.path.join

import requests
import json

from IPython.nbformat.current import (new_notebook, write, new_worksheet,
                              new_heading_cell, new_code_cell,
                              new_output)

from IPython.html.utils import url_path_join
from .launchnotebook import NotebookTestBase
from IPython.utils import py3compat


class FilesTest(NotebookTestBase):
    def test_hidden_files(self):
        not_hidden = [
            u'å b',
            u'å b/ç. d',
        ]
        hidden = [
            u'.å b',
            u'å b/.ç d',
        ]
        dirs = not_hidden + hidden
        
        nbdir = self.notebook_dir.name
        for d in dirs:
            path = pjoin(nbdir, d.replace('/', os.sep))
            if not os.path.exists(path):
                os.mkdir(path)
            with open(pjoin(path, 'foo'), 'w') as f:
                f.write('foo')
            with open(pjoin(path, '.foo'), 'w') as f:
                f.write('.foo')
        url = self.base_url()
        
        for d in not_hidden:
            path = pjoin(nbdir, d.replace('/', os.sep))
            r = requests.get(url_path_join(url, 'files', d, 'foo'))
            r.raise_for_status()
            self.assertEqual(r.text, 'foo')
            r = requests.get(url_path_join(url, 'files', d, '.foo'))
            self.assertEqual(r.status_code, 404)
            
        for d in hidden:
            path = pjoin(nbdir, d.replace('/', os.sep))
            for foo in ('foo', '.foo'):
                r = requests.get(url_path_join(url, 'files', d, foo))
                self.assertEqual(r.status_code, 404)
    
    def test_contents_manager(self):
        "make sure ContentsManager returns right files (ipynb, bin, txt)."

        nbdir = self.notebook_dir.name
        base = self.base_url()

        nb = new_notebook(name='testnb')
        
        ws = new_worksheet()
        nb.worksheets = [ws]
        ws.cells.append(new_heading_cell(u'Created by test ³'))
        cc1 = new_code_cell(input=u'print(2*6)')
        cc1.outputs.append(new_output(output_text=u'12', output_type='stream'))
        ws.cells.append(cc1)

        with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', 
            encoding='utf-8') as f:
            write(nb, f, format='ipynb')

        with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
            f.write(b'\xff' + os.urandom(5))
            f.close()

        with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
            f.write(u'foobar')
            f.close()

        r = requests.get(url_path_join(base, 'files', 'testnb.ipynb'))
        self.assertEqual(r.status_code, 200)
        self.assertIn('print(2*6)', r.text)
        json.loads(r.text)

        r = requests.get(url_path_join(base, 'files', 'test.bin'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'application/octet-stream')

        self.assertEqual("{:02x}".format(ord(r.content[0])), 'ff')
        self.assertEqual(len(r.content), 6)

        r = requests.get(url_path_join(base, 'files', 'test.txt'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'text/plain')
        self.assertEqual(r.text, 'foobar')


    def test_old_files_redirect(self):
        """pre-2.0 'files/' prefixed links are properly redirected"""
        nbdir = self.notebook_dir.name
        base = self.base_url()
        
        os.mkdir(pjoin(nbdir, 'files'))
        os.makedirs(pjoin(nbdir, 'sub', 'files'))<commit_msg>another shot at bin-tests on python3<commit_after># coding: utf-8
"""Test the /files/ handler."""

import io
import os
from unicodedata import normalize

pjoin = os.path.join

import requests
import json
import binascii

from IPython.nbformat.current import (new_notebook, write, new_worksheet,
                              new_heading_cell, new_code_cell,
                              new_output)

from IPython.html.utils import url_path_join
from .launchnotebook import NotebookTestBase
from IPython.utils import py3compat


class FilesTest(NotebookTestBase):
    def test_hidden_files(self):
        not_hidden = [
            u'å b',
            u'å b/ç. d',
        ]
        hidden = [
            u'.å b',
            u'å b/.ç d',
        ]
        dirs = not_hidden + hidden
        
        nbdir = self.notebook_dir.name
        for d in dirs:
            path = pjoin(nbdir, d.replace('/', os.sep))
            if not os.path.exists(path):
                os.mkdir(path)
            with open(pjoin(path, 'foo'), 'w') as f:
                f.write('foo')
            with open(pjoin(path, '.foo'), 'w') as f:
                f.write('.foo')
        url = self.base_url()
        
        for d in not_hidden:
            path = pjoin(nbdir, d.replace('/', os.sep))
            r = requests.get(url_path_join(url, 'files', d, 'foo'))
            r.raise_for_status()
            self.assertEqual(r.text, 'foo')
            r = requests.get(url_path_join(url, 'files', d, '.foo'))
            self.assertEqual(r.status_code, 404)
            
        for d in hidden:
            path = pjoin(nbdir, d.replace('/', os.sep))
            for foo in ('foo', '.foo'):
                r = requests.get(url_path_join(url, 'files', d, foo))
                self.assertEqual(r.status_code, 404)
    
    def test_contents_manager(self):
        "make sure ContentsManager returns right files (ipynb, bin, txt)."

        nbdir = self.notebook_dir.name
        base = self.base_url()

        nb = new_notebook(name='testnb')
        
        ws = new_worksheet()
        nb.worksheets = [ws]
        ws.cells.append(new_heading_cell(u'Created by test ³'))
        cc1 = new_code_cell(input=u'print(2*6)')
        cc1.outputs.append(new_output(output_text=u'12', output_type='stream'))
        ws.cells.append(cc1)

        with io.open(pjoin(nbdir, 'testnb.ipynb'), 'w', 
            encoding='utf-8') as f:
            write(nb, f, format='ipynb')

        with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
            f.write(b'\xff' + os.urandom(5))
            f.close()

        with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
            f.write(u'foobar')
            f.close()

        r = requests.get(url_path_join(base, 'files', 'testnb.ipynb'))
        self.assertEqual(r.status_code, 200)
        self.assertIn('print(2*6)', r.text)
        json.loads(r.text)

        r = requests.get(url_path_join(base, 'files', 'test.bin'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'application/octet-stream')

        self.assertEqual(binascii.hexlify(r.content[0]), 'ff')
        self.assertEqual(len(r.content), 6)

        r = requests.get(url_path_join(base, 'files', 'test.txt'))
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers['content-type'], 'text/plain')
        self.assertEqual(r.text, 'foobar')


    def test_old_files_redirect(self):
        """pre-2.0 'files/' prefixed links are properly redirected"""
        nbdir = self.notebook_dir.name
        base = self.base_url()
        
        os.mkdir(pjoin(nbdir, 'files'))
        os.makedirs(pjoin(nbdir, 'sub', 'files'))
{'fraction_non_alphanumeric': 0.14139484099904462, 'fraction_numerical': 0.009144260952640917, 'mean_word_length': 2.0495214315439036, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4129250', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2376, 'n_words': 486}