def test_site_plugin_with_additional_command(self): with common.TemporarySite(self) as site: site.add( "site.yaml", { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": {}, "build_steps": [], }) site.makedirs("plugins") site.add( "plugins/test_command.py", """ import logging import incontext def initialize_plugin(incontext): incontext.add_command("new-command", command_new_command) def command_new_command(incontext, parser): def new_command(options): logging.info("success!") return new_command """) site.run(["new-command"])
def test_ignore(self): with common.TemporarySite(self) as site: site.add( "site.yaml", { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": {}, "build_steps": [{ "task": "process_files", "args": { "handlers": [{ "when": [ ".*\.txt", ], "then": "ignore", }, { "when": [ ".*\.txt", ], "then": "copy_file", }], } }], }) site.touch("content/foo.txt") site.build() site.assertNotExists("build/files/foo.txt")
def test_transform_image_gif_to_jpeg(self): with common.TemporarySite(self) as site: site.add("templates/photo.html", "{{ page.title }}\n") site.add("site.yaml", SITE_CONFIGURATION_WITH_IMAGE_HANDLER) site.add( "media.yaml", { "profiles": { "default": [{ "where": "glob('*')", "transforms": [ "resize('preview', width=100, format='image/jpeg', sets=['preview'])", ], }], }, }) site.copy(PREVIEW_GIF, "content/image.gif") site.build() site.assertBuildFiles({"image/preview.jpg", "image/index.html"}) site.assertMIMEType("build/files/image/preview.jpg", "image/jpeg") self.assertEqual( site.store.get("/image/")['preview'], { 'width': 100.0, 'height': 65.0, 'filename': 'image/preview.jpg', 'url': '/image/preview.jpg' }) site.assertImageSize("build/files/image/preview.jpg", (100, 65))
def test_copy_file_multiple_regex_syntax(self): with common.TemporarySite(self) as site: site.add( "site.yaml", { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": {}, "build_steps": [{ "task": "process_files", "args": { "handlers": [{ "when": [ ".*\.txt", ".*\.jpeg", ], "then": "copy_file", }], } }], }) site.touch("content/foo.txt") site.touch("content/example.markdown") site.touch("content/image.jpeg") site.build() site.assertExists("build/files/foo.txt") site.assertExists("build/files/image.jpeg") site.assertNotExists("build/files/example.markdown")
def test_create_test_site(self): with common.TemporarySite(self, configuration={}) as site: self.assertIsNotNone(site.path) self.assertTrue( os.path.exists(os.path.join(site.path, "site.yaml"))) self.assertTrue(os.path.isdir(os.path.join(site.path, "content"))) self.assertTrue(os.path.isdir(os.path.join(site.path, "templates")))
def test_image_title_from_exif(self): with common.TemporarySite(self) as site: site.add("templates/photo.html", "{{ page.title }}") site.add("site.yaml", SITE_CONFIGURATION_WITH_IMAGE_HANDLER) site.copy(IMG_6218_TIFF, "content/image.tiff") site.build() site.assertExists("build/files/image/index.html") site.assertFileContents("build/files/image/index.html", "Theophany")
def test_transform_image_with_no_transforms(self): with common.TemporarySite(self) as site: site.add("templates/photo.html", "{{ page.title }}\n") site.add("site.yaml", SITE_CONFIGURATION_WITH_IMAGE_HANDLER) site.add("media.yaml", { "profiles": { "default": [], }, }) site.copy(IMG_3864_JPEG, "content/image.jpeg") with self.assertRaises(KeyError): site.build()
def test_transform_image_tiff_default_configuration(self): with common.TemporarySite(self) as site: site.add("templates/photo.html", "{{ page.title }}\n") site.add("site.yaml", SITE_CONFIGURATION_WITH_IMAGE_HANDLER) site.copy(IMG_6218_TIFF, "content/image.tiff") site.build() site.assertBuildFiles( {"image/thumbnail.jpg", "image/image.jpg", "image/index.html"}) site.assertMIMEType("build/files/image/image.jpg", "image/jpeg") site.assertImageSize("build/files/image/image.jpg", (1600, 1200)) site.assertMIMEType("build/files/image/thumbnail.jpg", "image/jpeg") site.assertImageSize("build/files/image/thumbnail.jpg", (480, 360))
def test_transform_image_gif_default_configuration(self): with common.TemporarySite(self) as site: site.add("templates/photo.html", "{{ page.title }}\n") site.add("site.yaml", SITE_CONFIGURATION_WITH_IMAGE_HANDLER) site.copy(PREVIEW_GIF, "content/image.gif") site.assertImageSize("content/image.gif", (800, 520)) site.build() site.assertBuildFiles( {"image/thumbnail.gif", "image/image.gif", "image/index.html"}) site.assertMIMEType("build/files/image/image.gif", "image/gif") site.assertImageSize( "build/files/image/image.gif", (800, 520)) # Images shouldn't be get larger than the initial size. site.assertMIMEType("build/files/image/thumbnail.gif", "image/gif") site.assertImageSize("build/files/image/thumbnail.gif", (480, 312))
def test_relative_img_src_rewrite(self): with common.TemporarySite(self) as site: site.add("site.yaml", SITE_CONFIGURATION_WITH_COMMON_HANDLERS) site.add("templates/photo.html", '{{ page.title }}') site.add("templates/post.html", '{{ page.html | safe }}') site.makedirs("content/gallery") site.copy(IMG_3864_JPEG, "content/gallery/image.jpeg") site.add("content/gallery/index.markdown", '<img src="image.jpeg">') site.build() site.assertExists("build/files/gallery/image/index.html") site.assertExists("build/files/gallery/image/image.jpeg") site.assertExists("build/files/gallery/image/thumbnail.jpeg") site.assertExists("build/files/gallery/index.html") site.assertFileContents( "build/files/gallery/index.html", '<p><img src="/gallery/image/image.jpeg" srcset></p>\n')
def test_add_draft_and_publish_with_build(self): with common.TemporarySite(self) as site: site.add( "site.yaml", { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": { "drafts": "content/drafts", "posts": "content/posts", }, "build_steps": [{ "task": "process_files", "args": { "handlers": [{ "when": "(.*/)?.*\.markdown", "then": "import_markdown", }], } }], }) site.add("templates/post.html", "{{ page.title }}\n") site.assertNotExists("build") site.run(["add", "draft", "Cheese is wonderful"]) site.assertExists( "content/drafts/cheese-is-wonderful/index.markdown") site.build() self.assertTrue( os.path.exists( os.path.join( site.path, "build/files/drafts/cheese-is-wonderful/index.html"))) site.run([ "publish", os.path.join(site.path, "content/drafts/cheese-is-wonderful") ]) today = datetime.date.today().strftime("%Y-%m-%d") site.assertIsDir(f"content/posts/{today}-cheese-is-wonderful") site.assertExists( f"content/posts/{today}-cheese-is-wonderful/index.markdown") site.build() site.assertNotExists( "build/files/drafts/cheese-is-wonderful/index.html") site.assertExists( f"build/files/posts/{today}-cheese-is-wonderful/index.html")
def test_transform_image_with_no_matching_transform(self): with common.TemporarySite(self) as site: site.add("templates/photo.html", "{{ page.title }}\n") site.add("site.yaml", SITE_CONFIGURATION_WITH_IMAGE_HANDLER) site.add( "media.yaml", { "profiles": { "default": [{ "where": "glob('*.tiff')", "transforms": [ "resize('preview', width=100, format='image/jpeg', sets=['preview'])", ], }], }, }) site.copy(IMG_3864_JPEG, "content/image.jpeg") with self.assertRaises(KeyError): site.build()
def test_loads_empty_site_plugin(self): with common.TemporarySite(self) as site: site.add( "site.yaml", { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": {}, "build_steps": [], }) site.makedirs("plugins") site.add("plugins/example.py", """ def initialize_plugin(incontext): pass """) site.build()
def test_relative_picture_source_srcset_rewrite(self): with common.TemporarySite(self) as site: site.add("site.yaml", SITE_CONFIGURATION_WITH_COMMON_HANDLERS) site.add("templates/photo.html", '{{ page.title }}') site.add("templates/post.html", '{{ page.html | safe }}') site.makedirs("content/gallery") site.copy(IMG_3864_JPEG, "content/gallery/image.jpeg") site.add( "content/gallery/index.markdown", '<picture><source srcset="image.jpeg" media="(prefers-color-scheme: dark)" /><source srcset="image.jpeg" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)" /><img src="image.jpeg" loading="lazy" /></picture>' ) site.build() site.assertExists("build/files/gallery/image/index.html") site.assertExists("build/files/gallery/image/image.jpeg") site.assertExists("build/files/gallery/image/thumbnail.jpeg") site.assertExists("build/files/gallery/index.html") site.assertFileContents( "build/files/gallery/index.html", '<p><picture><source srcset="/gallery/image/image.jpeg" media="(prefers-color-scheme: dark)"></source><source srcset="/gallery/image/image.jpeg" media="(prefers-color-scheme: light), (prefers-color-scheme: no-preference)"></source><img src="/gallery/image/image.jpeg" loading="lazy" srcset></picture></p>\n' )
def test_add_draft_and_publish_with_build(self): configuration = { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": { "drafts": "content/drafts", "posts": "content/posts", }, "build_steps": [ { "task": "process_files", "args": { "handlers": [ { "when": "(.*/)?.*\.markdown", "then": "import_markdown", } ], } }, ], } with common.TemporarySite(configuration=configuration) as site: with open(os.path.join(site.path, "templates", "post.html"), "w") as fh: fh.write("{{ page.title }}\n") self.assertFalse(os.path.exists(os.path.join(site.path, "build"))) common.run_incontext(["add", "draft", "Cheese is wonderful"], plugins_directory=paths.PLUGINS_DIR) self.assertTrue(os.path.exists(os.path.join(site.path, "content/drafts/cheese-is-wonderful/index.markdown"))) common.run_incontext(["build"], plugins_directory=paths.PLUGINS_DIR) self.assertTrue(os.path.exists(os.path.join(site.path, "build/files/drafts/cheese-is-wonderful/index.html"))) common.run_incontext(["publish", os.path.join(site.path, "content/drafts/cheese-is-wonderful")], plugins_directory=paths.PLUGINS_DIR) today = datetime.date.today().strftime("%Y-%m-%d") self.assertTrue(os.path.isdir(os.path.join(site.path, f"content/posts/{today}-cheese-is-wonderful"))) self.assertTrue(os.path.exists(os.path.join(site.path, f"content/posts/{today}-cheese-is-wonderful/index.markdown"))) common.run_incontext(["build"], plugins_directory=paths.PLUGINS_DIR) self.assertFalse(os.path.exists(os.path.join(site.path, "build/files/drafts/cheese-is-wonderful/index.html"))) self.assertTrue(os.path.exists(os.path.join(site.path, f"build/files/posts/{today}-cheese-is-wonderful/index.html")))
def test_build_and_clean_empty_site(self): with common.TemporarySite(self) as site: site.add( "site.yaml", { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": {}, "build_steps": [{ "task": "process_files", "args": { "handlers": [], } }], }) site.assertNotExists("build") site.build() site.assertIsDir("build") site.clean() site.assertNotExists("build")
def test_fail_to_load_invalid_site_plugin(self): with common.TemporarySite(self) as site: site.add( "site.yaml", { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": {}, "build_steps": [], }) site.makedirs("plugins") site.add( "plugins/example.py", """ def initialize_plugin(incontext): raise AssertionError("Boom!") """) with self.assertRaises(AssertionError): site.build()
def test_build_and_clean_empty_site(self): configuration = { "config": { "title": "Example Site", "url": "https://example.com" }, "paths": {}, "build_steps": [ { "task": "process_files", "args": { "handlers": [], } }, ], } with common.TemporarySite(configuration=configuration) as site: self.assertFalse(os.path.exists(os.path.join(site.path, "build"))) common.run_incontext(["build"], plugins_directory=paths.PLUGINS_DIR) self.assertTrue(os.path.isdir(os.path.join(site.path, "build"))) common.run_incontext(["clean"], plugins_directory=paths.PLUGINS_DIR) self.assertFalse(os.path.exists(os.path.join(site.path, "build")))