def template_metadata() -> Dict[str, Any]: """Load node specific template metadata. Returns: Dictionary of metadata. Includes the entire parsed contents of the package.json file if present. Other expected fields: * quickstart (str): Contents of the quickstart snippet if available, otherwise, "" * samples (List[Dict[str, str]]): List of available samples. See synthtool.gcp.samples.all_samples() """ metadata = {} try: metadata = read_metadata() except FileNotFoundError: pass all_samples = samples.all_samples(["samples/*.js"]) # quickstart.js sample is special - only include it in the samples list if there is # a quickstart snippet present in the file quickstart_snippets = list( snippets.all_snippets_from_file("samples/quickstart.js").values() ) metadata["quickstart"] = quickstart_snippets[0] if quickstart_snippets else "" metadata["samples"] = list( filter( lambda sample: sample["file"] != "samples/quickstart.js" or metadata["quickstart"], all_samples, ) ) return metadata
def test_non_existent_file(): cwd = os.getcwd() os.chdir(FIXTURES) all_snippets = snippets.all_snippets_from_file( "snippets/non-existent-file.foo") assert len(all_snippets) == 0 os.chdir(cwd)
def test_reused_tag(): with util.chdir(FIXTURES): all_snippets = snippets.all_snippets_from_file("snippets/reused.js") assert len(all_snippets) == 1 assert ( all_snippets["snippet_1"] == """var line1 = 1; var line3 = 3; """ )
def test_interleaving_snippets_with_exclude(): cwd = os.getcwd() os.chdir(FIXTURES) all_snippets = snippets.all_snippets_from_file( "snippets/interleaved_with_exclude.js") assert len(all_snippets) == 1 assert (all_snippets["snippet_1"] == """var line1 = 1; var line3 = 3; """) os.chdir(cwd)
def test_nested_snippets(): cwd = os.getcwd() os.chdir(FIXTURES) all_snippets = snippets.all_snippets_from_file("snippets/nested.js") assert len(all_snippets) == 2 assert (all_snippets["nested_snippet_1"] == """var line1 = 1; var line2 = 2; var line3 = 3; """) assert (all_snippets["nested_snippet_2"] == """var line2 = 2; """) os.chdir(cwd)
def test_nested_snippets(): with util.chdir(FIXTURES): all_snippets = snippets.all_snippets_from_file("snippets/nested.js") assert len(all_snippets) == 2 assert ( all_snippets["nested_snippet_1"] == """var line1 = 1; var line2 = 2; var line3 = 3; """ ) assert ( all_snippets["nested_snippet_2"] == """var line2 = 2; """ )
def test_non_existent_file(): with util.chdir(FIXTURES): all_snippets = snippets.all_snippets_from_file("snippets/non-existent-file.foo") assert len(all_snippets) == 0