def get_level(self, path: str, args: argparse.Namespace) -> Optional[str]: """Get level to scan package at.""" path = os.path.abspath(path) profile_filename = "profile.yaml" if args.profile is not None: profile_filename = args.profile profile_resource = self.resources.get_file(profile_filename) if profile_resource is None: logging.error("Could not find profile file %s!", profile_filename) return None try: profile = Profile(profile_resource) except OSError as ex: # This isn't quite redundant with the profile_resource check: it's possible # that something else triggers an OSError, like permissions. logging.error("Failed to access profile file %s: %s", profile_filename, ex) return None except ValueError as ex: logging.error("Profile file %s has errors: %s", profile_filename, ex) return None package = Package(os.path.basename(path), path) level = profile.get_package_level(package) return level
def get_level(self, path, args): """Get level to scan package at.""" path = os.path.abspath(path) profile_filename = "profile.yaml" if args.profile is not None: profile_filename = args.profile profile_resource = self.resources.get_file(profile_filename) if profile_resource is None: print("Could not find profile file {}!".format(profile_filename)) return None try: profile = Profile(profile_resource) except OSError as ex: # This isn't quite redundant with the profile_resource check: it's possible # that something else triggers an OSError, like permissions print("Failed to access profile file {}: {}".format(profile_filename, ex)) return None except ValueError as ex: print("Profile file {} has errors: {}".format(profile_filename, ex)) return None package = Package(os.path.basename(path), path) level = profile.get_package_level(package) return level
def test_profile_get_package_level_validpackage(): """ Test for when get_package_level is called with a package not in the packages list. Expected result: the package-specific value is returned """ package = Package('package', os.path.dirname(__file__)) profile = Profile(os.path.join(os.path.dirname(__file__), 'profile.yaml')) assert profile.get_package_level(package) == "package_specific"
def test_profile_get_package_level_invalidpackage(): """ Test for when get_package_level is called with a package not in the packages list. Expected result: default is returned """ package = Package("nopackage", os.path.dirname(__file__)) profile = Profile(os.path.join(os.path.dirname(__file__), "profile.yaml")) assert profile.get_package_level(package) == "default_value"
def test_profile_get_package_level_nopackage(): """ Test for when get_package_level is called with no packages defined. Expected result: default is returned """ package = Package("test", os.path.dirname(__file__)) profile = Profile(os.path.join(os.path.dirname(__file__), "profile-nopackage.yaml")) assert profile.get_package_level(package) == "default_value"
def test_profile_init(): """ Test that the Profile module initializes correctly. Expected result: profile is initialized """ profile = Profile(os.path.join(os.path.dirname(__file__), 'profile.yaml')) assert profile.profile
def test_profile_bad_yaml(): """ Test for when a Profile is initialized with something that isn't a valid YAML file. Expected result: ValueError is thrown """ with pytest.raises(ValueError): Profile(os.path.join(os.path.dirname(__file__), 'bad.yaml'))
def test_profile_nodefault(): """ Test for when a Profile is initialized with a YAML that doesn't have a 'default' key. Expected result: ValueError is thrown """ with pytest.raises(ValueError): Profile(os.path.join(os.path.dirname(__file__), 'nodefault.yaml'))
def test_profile_empty(): """ Test for when a Profile is initialized with an empty YAML. Expected result: ValueError is thrown """ with pytest.raises(ValueError): Profile(os.path.join(os.path.dirname(__file__), 'empty.yaml'))
def test_profile_file_empty_string(): """ Test for when a Profile is initialized with an empty string. Expected result: ValueError is thrown """ with pytest.raises(ValueError): Profile(os.path.join(""))
def test_profile_nonexistent(): """ Test for when a Profile is initialized with a nonexistent YAML. Expected result: OSError is thrown """ with pytest.raises(OSError): Profile(os.path.join(os.path.dirname(__file__), 'nope.yaml'))
def get_level(self, path, args): """Get level to scan package at.""" path = os.path.abspath(path) profile_filename = "profile.yaml" if args.profile is not None: profile_filename = args.profile try: profile = Profile(self.resources.get_file(profile_filename)) except TypeError: print("Could not find profile file {}!".format(profile_filename)) return None package = Package(os.path.basename(path), path) level = profile.get_package_level(package) return level