Ejemplo n.º 1
0
def test_render_with_subdir():
    t = templates.Templates(FIXTURES)
    result = t.render("example.j2", name="world", subdir="foo/bar")

    assert result.match("**/foo/bar/example")
    assert result.name == "example"
    assert result.read_text() == "Hello, world!\n"
Ejemplo n.º 2
0
def test_samples_usage():
    t = templates.Templates(PYTHON_SAMPLES)
    result = t.render(
        "README.md",
        metadata={
            "repo": {
                "name_pretty":
                "Bigtable",
                "repo":
                "googleapis/python-bigtable",
                "product_documentation":
                "https://cloud.google.com/bigtable",
                "requires_billing":
                True,
                "samples": [
                    {
                        "name": "Quickstart",
                        "description": "Example sample for product",
                        "file": "quickstart.py",
                    },
                    {
                        "name": "Hello World",
                        "description": "Example beginner application",
                        "file": "main.py",
                    },
                ],
            }
        },
    ).read_text()
    assert "Example sample for product" in result
    assert "Example beginner application" in result
    assert "Hello World" in result
    assert '<a href="https://cloud.google.com/bigtable">' in result
Ejemplo n.º 3
0
def test_samples_billing():
    t = templates.Templates(PYTHON_SAMPLES)
    result = t.render(
        "README.md",
        metadata={
            "repo": {
                "name_pretty":
                "Bigtable",
                "repo":
                "googleapis/python-bigtable",
                "requires_billing":
                True,
                "samples": [
                    {
                        "name": "Quickstart",
                        "description": "Example sample for product",
                        "file": "quickstart.py",
                        "custom_content": "This is custom text for the sample",
                        "runnable": True,
                    },
                    {
                        "name": "Hello World",
                        "description": "Example beginner application",
                        "file": "main.py",
                    },
                ],
            }
        },
    ).read_text()
    assert "and you will need to [enable billing]" in result
Ejemplo n.º 4
0
    def __init__(self):
        if LOCAL_TEMPLATES:
            logger.debug(f"Using local templates at {LOCAL_TEMPLATES}")
            self._template_root = LOCAL_TEMPLATES
        else:
            templates_git = git.clone(TEMPLATES_URL)
            self._template_root = templates_git / DEFAULT_TEMPLATES_PATH

        self._templates = templates.Templates(self._template_root)
        self.excludes = []  # type: List[str]
Ejemplo n.º 5
0
def test_library_noxfile(template_kwargs, expected_text):
    t = templates.Templates(PYTHON_LIBRARY)
    result = t.render(
        "noxfile.py.j2",
        **template_kwargs,
    ).read_text()
    # Validate Python syntax.
    result_code = compile(result, "noxfile.py", "exec")
    assert result_code is not None
    for expected in expected_text:
        assert expected in result
Ejemplo n.º 6
0
def test_release_quality_badge():
    t = templates.Templates(NODE_TEMPLATES)
    result = t.render("README.md",
                      metadata={
                          "repo": {
                              "release_level": "beta"
                          },
                          "samples": {}
                      }).read_text()
    assert "https://img.shields.io/badge/release%20level-beta-yellow.svg" in result
    assert "This library is considered to be in **beta**" in result
Ejemplo n.º 7
0
    def __init__(self, template_path: Optional[Path] = None):
        if template_path:
            self._template_root = template_path
        elif LOCAL_TEMPLATES:
            logger.debug(f"Using local templates at {LOCAL_TEMPLATES}")
            self._template_root = Path(LOCAL_TEMPLATES)
        else:
            templates_git = git.clone(TEMPLATES_URL)
            self._template_root = templates_git / DEFAULT_TEMPLATES_PATH

        self._templates = templates.Templates(self._template_root)
        self.excludes = []  # type: List[str]
Ejemplo n.º 8
0
def test_syntax_highlighter():
    t = templates.Templates(NODE_TEMPLATES)
    result = t.render(
        "README.md",
        metadata={
            "repo": {
                "language": "nodejs"
            },
            "quickstart": "const foo = 'bar'"
        },
    ).read_text()
    assert "```javascript" in result
Ejemplo n.º 9
0
def test_render_preserve_mode():
    """
    Test that rendering templates correctly preserve file modes.
    """
    source_file = FIXTURES / "executable.j2"
    source_mode = source_file.stat().st_mode

    # Verify source fixture has execute permission for USER
    assert source_mode & stat.S_IXUSR

    t = templates.Templates(FIXTURES)
    result = t.render("executable.j2", name="executable")

    assert result.stat().st_mode == source_mode
Ejemplo n.º 10
0
def test_samples_noxfile():
    import shutil

    t = templates.Templates(PYTHON_SAMPLES)
    result = t.render("noxfile.py.j2").read_text()
    # Validate Python syntax.
    result_code = compile(result, "noxfile.py", "exec")
    assert result_code is not None
    # write rendered template to a file
    result_code_temp_file = tempfile.NamedTemporaryFile()
    result_code_temp_file.write(bytes(result, "utf-8"))
    # run flake8, which will check for missing imports
    # test fails if flake8 fails, no need to assert
    subprocess.check_call([shutil.which("flake8"), result_code_temp_file.name])
Ejemplo n.º 11
0
def test_ruby_authentication():
    t = templates.Templates(RUBY_TEMPLATES)
    # .repo-metadata.json in google-cloud-ruby package directories
    repo_metadata = {
        "distribution_name": "google-cloud-bigquery-data_transfer",
        "module_name": "Bigquery::DataTransfer",
        "module_name_credentials": "Bigquery::DataTransfer::V1",
        "env_var_prefix": "DATA_TRANSFER",
    }
    metadata = {"repo": repo_metadata}
    result = t.render("AUTHENTICATION.md", metadata=metadata).read_text()

    assert 'require "google/cloud/bigquery/data_transfer"' in result
    assert "Google::Cloud::Bigquery::DataTransfer.new" in result
    assert "Google::Cloud::Bigquery::DataTransfer::V1::Credentials" in result
    assert "DATA_TRANSFER_CREDENTIALS" in result
Ejemplo n.º 12
0
def test_samples_footer():
    t = templates.Templates(PYTHON_SAMPLES)
    result = t.render(
        "README.md",
        metadata={
            "repo": {
                "name_pretty": "Storage",
                "repo": "googleapis/python-storage",
                "client_library": True,
            }
        },
    ).read_text()
    assert "Google Cloud Client Library for Python" in result
    assert "Python style guide" in result
    assert (
        '<a href="https://github.com/googleapis/python-storage">googleapis/python-storage</a>'
        in result)
Ejemplo n.º 13
0
def test_hide_billing():
    t = templates.Templates(NODE_TEMPLATES)

    result = t.render("README.md",
                      metadata={
                          "repo": {
                              "requires_billing": True,
                              "api_id": "fooapi"
                          }
                      }).read_text()
    assert "Enable billing for your project" in result

    result = t.render("README.md",
                      metadata={
                          "repo": {
                              "requires_billing": False
                          }
                      }).read_text()
    assert "Enable billing for your project" not in result
Ejemplo n.º 14
0
def test_samples_custom_content():
    t = templates.Templates(PYTHON_SAMPLES)
    result = t.render(
        "README.md",
        metadata={
            "repo": {
                "name_pretty":
                "Storage",
                "repo":
                "googleapis/python-storage",
                "samples": [{
                    "name": "Quickstart",
                    "description": "Example sample for product",
                    "file": "quickstart.py",
                    "custom_content": "This is custom text for the sample",
                    "runnable": True,
                }],
            }
        },
    ).read_text()
    assert "This is custom text for the sample" in result
Ejemplo n.º 15
0
def test_configure_previous_major_version_branches(fixtures_dir):
    with util.copied_fixtures_dir(fixtures_dir):
        t = templates.Templates(PYTHON_LIBRARY)
        result = t.render(".github/release-please.yml")
        os.makedirs(".github")
        shutil.copy(result, Path(".github/release-please.yml"))

        python.configure_previous_major_version_branches()
        release_please_yml = Path(".github/release-please.yml").read_text()

        assert (release_please_yml == """releaseType: python
handleGHRelease: true
# NOTE: this section is generated by synthtool.languages.python
# See https://github.com/googleapis/synthtool/blob/master/synthtool/languages/python.py
branches:
- branch: v1
  handleGHRelease: true
  releaseType: python
- branch: v0
  handleGHRelease: true
  releaseType: python
""")
Ejemplo n.º 16
0
def test_samples_cloudshell():
    t = templates.Templates(PYTHON_SAMPLES)
    result = t.render(
        "README.md",
        metadata={
            "repo": {
                "name_pretty":
                "Bigtable",
                "repo":
                "googleapis/python-bigtable",
                "requires_billing":
                True,
                "samples": [{
                    "name": "Quickstart",
                    "description": "Example sample for product",
                    "file": "quickstart.py",
                    "runnable": True,
                }],
            }
        },
    ).read_text()
    assert "git_repo=https://github.com/googleapis/python-bigtable" in result
    assert "open_in_editor=quickstart.py" in result
Ejemplo n.º 17
0
def test_render():
    t = templates.Templates(FIXTURES)
    result = t.render("example.j2", name="world")

    assert result.name == "example"
    assert result.read_text() == "Hello, world!\n"
Ejemplo n.º 18
0
 def __init__(self):
     self._templates = templates.Templates(_TEMPLATES_DIR)
     self.excludes = []  # type: List[str]
Ejemplo n.º 19
0
 def __init__(self):
     self._templates = templates.Templates(_TEMPLATES_DIR)