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)
def _load_samples(self, metadata: Dict): """ walks samples directory and builds up samples data-structure: { "name": "Requester Pays", "file": "requesterPays.js" } """ metadata["samples"] = [] samples_dir = Path(os.getcwd()) / "samples" if os.path.exists(samples_dir): files = os.listdir(samples_dir) files.sort() for file in files: if re.match(r"[\w.\-]+\.js$", file): if file == "quickstart.js": metadata["quickstart"] = self._read_quickstart( samples_dir) # only add quickstart file to samples list if code sample is found. if file == "quickstart.js" and not metadata.get( "quickstart", None): continue sample_metadata = { "title": decamelize(file[:-3]), "file": file } sample_metadata.update( self._read_sample_metadata_comment(samples_dir, file)) metadata["samples"].append(sample_metadata)
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"
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"
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")
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"
def test_new_files_found(source_tree, preserve_track_obsolete_file_flag): metadata.set_track_obsolete_files(True) source_tree.write("a") with metadata.MetadataTrackerAndWriter(source_tree.tmpdir / "synth.metadata"): source_tree.write("code/b") # Confirm add_new_files found the new files and ignored the old one. new_file_paths = [path for path in metadata.get().generated_files] assert ["code/b"] == new_file_paths
def testClone(self): local_directory = git.clone("https://github.com/googleapis/nodejs-vision.git") self.assertEqual("nodejs-vision", local_directory.name) self.assertEqual("nodejs-vision", metadata.get().sources[0].git.name) # When the repo already exists, it should pull instead. same_local_directory = git.clone( "https://github.com/googleapis/nodejs-vision.git", local_directory.parent ) self.assertEqual(local_directory, same_local_directory)
def test_gitignored_files_ignored(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") source_tree.write(".gitignore", "code/c\n") # Confirm add_new_files found the new files and ignored one. new_file_paths = [new_file.path for new_file in metadata.get().new_files] assert [".gitignore", "code/b"] == new_file_paths
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"
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")
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)
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")
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
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