Пример #1
0
 def testPrecloneMap(self):
     # Pre clone the repo into a temporary directory.
     tmpdir = tmp.tmpdir()
     local_directory = git.clone(
         "https://github.com/googleapis/nodejs-vision.git", tmpdir
     )
     # Write out a preclone map.
     preconfig_path = tmpdir / "preconfig.json"
     preconfig = Preconfig()
     preconfig.precloned_repos[
         "https://github.com/googleapis/nodejs-vision.git"
     ] = str(local_directory)
     preconfig_path.write_text(google.protobuf.json_format.MessageToJson(preconfig))
     # Reload the module so it reexamines the environment variable.
     importlib.reload(synthtool.preconfig)
     metadata.reset()
     # Confirm calling clone with the preclone map returns the precloned local directory.
     os.environ[synthtool.preconfig.PRECONFIG_ENVIRONMENT_VARIABLE] = str(
         preconfig_path
     )
     same_local_directory = git.clone(
         "https://github.com/googleapis/nodejs-vision.git"
     )
     self.assertEqual(local_directory, same_local_directory)
     # Make sure it was recorded in the metadata.
     self.assertEqual("nodejs-vision", metadata.get().sources[0].git.name)
Пример #2
0
def test_add_generator_source():
    metadata.reset()

    metadata.add_generator_source(name="name", version="1.2.3")

    current = metadata.get()

    assert current.sources[0].generator.name == "name"
    assert current.sources[0].generator.version == "1.2.3"
Пример #3
0
def test_add_template_source():
    metadata.reset()

    metadata.add_template_source(name="name", version="1.2.3")

    current = metadata.get()

    assert current.sources[0].template.name == "name"
    assert current.sources[0].template.version == "1.2.3"
Пример #4
0
def test_add_git_source():
    metadata.reset()

    metadata.add_git_source(sha="sha", name="name", remote="remote")

    current = metadata.get()

    assert current.sources[0].git.sha == "sha"
    assert current.sources[0].git.name == "name"
    assert current.sources[0].git.remote == "remote"
Пример #5
0
def test_add_client_destination():
    metadata.reset()

    add_sample_client_destination()

    current = metadata.get()

    assert current.destinations[0].client.source == "source"
    assert current.destinations[0].client.api_name == "api"
    assert current.destinations[0].client.api_version == "v1"
    assert current.destinations[0].client.language == "py"
    assert current.destinations[0].client.generator == "gen"
    assert current.destinations[0].client.config == "config"
Пример #6
0
def test_old_file_ignored_by_git_not_removed(
    source_tree, preserve_track_obsolete_file_flag
):
    metadata.set_track_obsolete_files(True)

    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        source_tree.write(".bin")

    metadata.reset()
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        source_tree.write(".gitignore", ".bin")

    # Confirm remove_obsolete_files didn't remove the .bin file.
    assert os.path.exists(".bin")
Пример #7
0
def test_write(tmpdir):
    metadata.reset()

    metadata.add_git_source(sha="sha", name="name", remote="remote")

    output_file = tmpdir / "synth.metadata"

    metadata.write(str(output_file))

    data = output_file.read()

    # Ensure the file was written, that *some* metadata is in it, and that it
    # is valid JSON.
    assert data
    assert "sha" in data
    assert json.loads(data)
Пример #8
0
def test_old_file_removed(source_tree, preserve_track_obsolete_file_flag):
    metadata.set_track_obsolete_files(True)

    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        source_tree.write("code/b")
        source_tree.write("code/c")

    metadata.reset()
    time.sleep(1)
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        source_tree.write("code/c")

    assert 1 == len(metadata.get().new_files)
    assert "code/c" == metadata.get().new_files[0].path

    # Confirm remove_obsolete_files deletes b but not c.
    assert not os.path.exists("code/b")
    assert os.path.exists("code/c")
Пример #9
0
def test_nothing_happens_when_disabled(source_tree, preserve_track_obsolete_file_flag):
    metadata.set_track_obsolete_files(True)

    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        source_tree.write("code/b")
        source_tree.write("code/c")

    metadata.reset()
    time.sleep(1)
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        source_tree.write("code/c")
        metadata.set_track_obsolete_files(False)

    assert 0 == len(metadata.get().new_files)

    # Confirm no files were deleted.
    assert os.path.exists("code/b")
    assert os.path.exists("code/c")
Пример #10
0
def test_used_to_append_git_log_to_metadata(source_tree):
    """Synthtool used to append the git log for each git source.  But nothing
    consumes the log, and there's no design for anything to consume the log.
    Plus, it cluttered up synth.metadata.
    """
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                           "synth.metadata"):
        # Create one commit that will be recorded in the metadata.
        source_tree.write("a")
        source_tree.git_add("a")
        source_tree.git_commit("a")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    metadata.reset()
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                           "synth.metadata"):
        # Create two more commits that should appear in metadata git log.
        source_tree.write("code/b")
        source_tree.git_add("code/b")
        source_tree.git_commit("code/b")

        source_tree.write("code/c")
        source_tree.git_add("code/c")
        source_tree.git_commit("code/c")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    # Read the metadata that we just wrote.
    mdata = metadata._read_or_empty(source_tree.tmpdir / "synth.metadata")
    # The log should be empty.
    assert "" == mdata.sources[1].git.log
    # Make sure the local path field is not recorded.
    assert not mdata.sources[0].git.local_path is None
Пример #11
0
def test_append_git_log_to_metadata(source_tree):
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        # Create one commit that will be recorded in the metadata.
        source_tree.write("a")
        source_tree.git_add("a")
        source_tree.git_commit("a")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    metadata.reset()
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        # Create two more commits that should appear in metadata git log.
        source_tree.write("code/b")
        source_tree.git_add("code/b")
        source_tree.git_commit("code/b")

        source_tree.write("code/c")
        source_tree.git_add("code/c")
        source_tree.git_commit("code/c")

        hash = subprocess.run(
            [source_tree.git, "log", "-1", "--pretty=format:%H"],
            stdout=subprocess.PIPE,
            universal_newlines=True,
        ).stdout.strip()
        metadata.add_git_source(name="tmp", local_path=os.getcwd(), sha=hash)

    # Read the metadata that we just wrote.
    mdata = metadata._read_or_empty(source_tree.tmpdir / "synth.metadata")
    # Match 2 log lines.
    assert re.match(
        r"[0-9A-Fa-f]+\ncode/c\n+[0-9A-Fa-f]+\ncode/b\n+",
        mdata.sources[0].git.log,
        re.MULTILINE,
    )
    # Make sure the local path field is not recorded.
    assert not mdata.sources[0].git.local_path is None
Пример #12
0
def test_excluded_file_not_removed(source_tree,
                                   preserve_track_obsolete_file_flag):
    metadata.set_track_obsolete_files(True)
    _tracked_paths.add(source_tree.tmpdir / "build")

    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                           "synth.metadata"):
        source_tree.write("code/b")
        source_tree.write("code/c")

    metadata.reset()
    # Create a second source tree and copy it into the first.
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                           "synth.metadata"):
        # exclude code/c from being copied should mean it doesn't get deleted.
        transforms.move(source_tree.tmpdir / "build", excludes=["code/c"])

    # Confirm remove_obsolete_files deletes b but not c.
    assert not os.path.exists("code/b")
    assert os.path.exists("code/c")
Пример #13
0
def test_add_new_files_with_bad_file(source_tree, preserve_track_obsolete_file_flag):
    metadata.set_track_obsolete_files(True)

    metadata.reset()
    tmpdir = source_tree.tmpdir
    dne = "does-not-exist"
    source_tree.git_add(dne)
    time.sleep(1)  # File systems have resolution of about 1 second.

    try:
        os.symlink(tmpdir / dne, tmpdir / "badlink")
    except OSError:
        # On Windows, creating a symlink requires Admin priveleges, which
        # should never be granted to test runners.
        assert "win32" == sys.platform
        return
    # Confirm this doesn't throw an exception.
    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"):
        pass
    # And a bad link does not exist and shouldn't be recorded as a new file.
    assert 0 == len(metadata.get().new_files)
Пример #14
0
def test_nothing_happens_when_exception(source_tree,
                                        preserve_track_obsolete_file_flag):
    metadata.set_track_obsolete_files(True)

    with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                           "synth.metadata"):
        source_tree.write("code/b")
        source_tree.write("code/c")

    metadata.reset()
    try:
        with metadata.MetadataTrackerAndWriter(source_tree.tmpdir /
                                               "synth.metadata"):
            source_tree.write("code/c")
            raise "Exception!"
    except:  # noqa: E722
        pass

    assert 0 == len(metadata.get().new_files)

    # Confirm no files were deleted.
    assert os.path.exists("code/b")
    assert os.path.exists("code/c")
Пример #15
0
def test_git_sources_are_sorted(source_tree: SourceTree):
    metadata_path = source_tree.tmpdir / "synth.metadata"
    with metadata.MetadataTrackerAndWriter(metadata_path):
        metadata.add_generator_source(name="a-generator",
                                      version="1",
                                      docker_image="x")
        metadata.add_generator_source(name="b-generator",
                                      version="2",
                                      docker_image="y")
        metadata.add_template_source(name="a-template",
                                     origin="andromeda",
                                     version="3")
        metadata.add_template_source(name="b-template",
                                     origin="milky way",
                                     version="4")
        metadata.add_git_source(name="a-git", sha="1a")
        metadata.add_git_source(name="b-git", sha="1b")
    m1 = metadata._read_or_empty(metadata_path)
    # Add the same sources in reverse order.
    metadata.reset()
    with metadata.MetadataTrackerAndWriter(metadata_path):
        metadata.add_git_source(name="b-git", sha="1b")
        metadata.add_git_source(name="a-git", sha="1a")
        metadata.add_template_source(name="b-template",
                                     origin="milky way",
                                     version="4")
        metadata.add_template_source(name="a-template",
                                     origin="andromeda",
                                     version="3")
        metadata.add_generator_source(name="b-generator",
                                      version="2",
                                      docker_image="y")
        metadata.add_generator_source(name="a-generator",
                                      version="1",
                                      docker_image="x")
    m2 = metadata._read_or_empty(metadata_path)
    assert m1.sources == m2.sources
Пример #16
0
def test_read_nonexistent_metadata(tmpdir):
    # The file doesn't exist.
    read_metadata = metadata._read_or_empty(tmpdir / "synth.metadata")
    metadata.reset()
    assert metadata.get() == read_metadata
Пример #17
0
def test_read_metadata(tmpdir):
    metadata.reset()
    add_sample_client_destination()
    metadata.write(tmpdir / "synth.metadata")
    read_metadata = metadata._read_or_empty(tmpdir / "synth.metadata")
    assert metadata.get() == read_metadata
Пример #18
0
 def __init__(self, tmpdir):
     metadata.reset()
     self.tmpdir = tmpdir
     self.git = shutil.which("git")
     subprocess.run([self.git, "init"])