Best Inspec_ruby code snippet using FilePermissionsSelector.contain
file.rb
Source:file.rb
...51 res = file.content52 return nil if res.nil?53 res.force_encoding('utf-8')54 end55 def contain(*_)56 raise 'Contain is not supported. Please use standard RSpec matchers.'57 end58 def readable?(by_usergroup, by_specific_user)59 return false unless exist?60 return skip_resource '`readable?` is not supported on your OS yet.' if @perms_provider.nil?61 file_permission_granted?('read', by_usergroup, by_specific_user)62 end63 def writable?(by_usergroup, by_specific_user)64 return false unless exist?65 return skip_resource '`writable?` is not supported on your OS yet.' if @perms_provider.nil?66 file_permission_granted?('write', by_usergroup, by_specific_user)67 end68 def executable?(by_usergroup, by_specific_user)69 return false unless exist?70 return skip_resource '`executable?` is not supported on your OS yet.' if @perms_provider.nil?71 file_permission_granted?('execute', by_usergroup, by_specific_user)72 end73 def mounted?(expected_options = nil, identical = false)74 mounted = file.mounted75 # return if no additional parameters have been provided76 return file.mounted? if expected_options.nil?77 # deprecation warning, this functionality will be removed in future version78 warn "[DEPRECATION] `be_mounted.with and be_mounted.only_with` are deprecated. Please use `mount('#{source_path}')` instead."79 # we cannot read mount data on non-Linux systems80 return nil if !inspec.os.linux?81 # parse content if we are on linux82 @mount_options ||= parse_mount_options(mounted.stdout, true)83 if identical84 # check if the options should be identical85 @mount_options == expected_options86 else87 # otherwise compare the selected values88 @mount_options.contains(expected_options)89 end90 end91 def suid92 (mode & 04000) > 093 end94 def sgid95 (mode & 02000) > 096 end97 def sticky98 (mode & 01000) > 099 end100 def to_s101 "File #{source_path}"102 end103 private104 def file_permission_granted?(access_type, by_usergroup, by_specific_user)105 raise '`file_permission_granted?` is not supported on your OS' if @perms_provider.nil?106 if by_specific_user.nil? || by_specific_user.empty?107 @perms_provider.check_file_permission_by_mask(file, access_type, by_usergroup, by_specific_user)108 else109 @perms_provider.check_file_permission_by_user(access_type, by_specific_user, source_path)110 end111 end112 end113 class FilePermissions114 attr_reader :inspec115 def initialize(inspec)116 @inspec = inspec117 end118 end119 class UnixFilePermissions < FilePermissions120 def permission_flag(access_type)121 case access_type122 when 'read'123 'r'124 when 'write'125 'w'126 when 'execute'127 'x'128 else129 raise 'Invalid access_type provided'130 end131 end132 def usergroup_for(usergroup, specific_user)133 if usergroup == 'others'134 'other'135 elsif (usergroup.nil? || usergroup.empty?) && specific_user.nil?136 'all'137 else138 usergroup139 end140 end141 def check_file_permission_by_mask(file, access_type, usergroup, specific_user)142 usergroup = usergroup_for(usergroup, specific_user)143 flag = permission_flag(access_type)144 mask = file.unix_mode_mask(usergroup, flag)145 raise 'Invalid usergroup/owner provided' if mask.nil?146 (file.mode & mask) != 0147 end148 def check_file_permission_by_user(access_type, user, path)149 flag = permission_flag(access_type)150 if inspec.os.linux?151 perm_cmd = "su -s /bin/sh -c \"test -#{flag} #{path}\" #{user}"152 elsif inspec.os.bsd? || inspec.os.solaris?153 perm_cmd = "sudo -u #{user} test -#{flag} #{path}"154 elsif inspec.os.aix?155 perm_cmd = "su #{user} -c test -#{flag} #{path}"156 elsif inspec.os.hpux?157 perm_cmd = "su #{user} -c \"test -#{flag} #{path}\""158 else159 return skip_resource 'The `file` resource does not support `by_user` on your OS.'160 end161 cmd = inspec.command(perm_cmd)162 cmd.exit_status == 0 ? true : false163 end164 end165 class WindowsFilePermissions < FilePermissions166 def check_file_permission_by_mask(_file, _access_type, _usergroup, _specific_user)167 raise '`check_file_permission_by_mask` is not supported on Windows'168 end169 def check_file_permission_by_user(access_type, user, path)170 access_rule = case access_type171 when 'read'172 '@(\'FullControl\', \'Modify\', \'ReadAndExecute\', \'Read\', \'ListDirectory\')'173 when 'write'174 '@(\'FullControl\', \'Modify\', \'Write\')'175 when 'execute'176 '@(\'FullControl\', \'Modify\', \'ReadAndExecute\', \'ExecuteFile\')'177 else178 raise 'Invalid access_type provided'179 end180 cmd = inspec.command("@(@((Get-Acl '#{path}').access | Where-Object {$_.AccessControlType -eq 'Allow' -and $_.IdentityReference -eq '#{user}' }) | Where-Object {($_.FileSystemRights.ToString().Split(',') | % {$_.trim()} | ? {#{access_rule} -contains $_}) -ne $null}) | measure | % { $_.Count }")181 cmd.stdout.chomp == '0' ? false : true182 end183 end184end...
contain
Using AI Code Generation
1tar_file = File.new('test.tar', File::CREAT|File::RDWR)2writer = Gem::Package::TarWriter.new(tar_file)3writer.add_file('file1', 0644) do |f|4writer.add_file('file2', 0644) do |f|5tar_file = File.new('test.tar', File::RDONLY)6reader = Gem::Package::TarReader.new(tar_file)7File.delete('test.tar')8tar_file = File.new('test.tar', File::CREAT|File::RDWR)9writer = Gem::Package::TarWriter.new(tar_file)10writer.add_file('file1', 0644) do |f|11writer.add_file('file2', 0644) do |f|12tar_file = File.new('test.tar', File::RDONLY)13reader = Gem::Package::TarReader.new(tar_file)14File.delete('test.tar')15tar_file = File.new('test.tar', File::CREAT|File::RDWR)16writer = Gem::Package::TarWriter.new(tar_file)17writer.add_file('file1', 0644) do |f|18writer.add_file('file2', 0644) do |f|
contain
Using AI Code Generation
1filePermissions = FilePermissions.new(1, 2, 3)2filePermissionsSelector = FilePermissionsSelector.new(filePermissions)3if filePermissionsSelector.contain(filePermissions)4filePermissions = FilePermissions.new(1, 2, 3)5filePermissionsSelector = FilePermissionsSelector.new(filePermissions)6if filePermissions.contain(filePermissionsSelector)
contain
Using AI Code Generation
1selector = FilePermissionsSelector.new(0755)2Find.find('/usr/bin') { |file|3 puts file if selector.contains?(file)4}
contain
Using AI Code Generation
1FileUtils.touch("test.txt")2FileUtils.chmod(0600, "test.txt")3if f.contain?(0600, "test.txt")4FileUtils.chmod(0644, "test.txt")5if f.contain?(0644, "test.txt")6FileUtils.chmod(0777, "test.txt")7if f.contain?(0777, "test.txt")8FileUtils.chmod(0755, "test.txt")9if f.contain?(0755, "test.txt")10FileUtils.chmod(0666, "test.txt")11if f.contain?(0666, "test.txt")12FileUtils.chmod(0444, "test.txt")13if f.contain?(0444, "test.txt")14FileUtils.chmod(0222, "test.txt")15if f.contain?(0222, "test.txt")16FileUtils.chmod(0111, "test.txt")
contain
Using AI Code Generation
1FileWatcher.new( '.', 2 :filter => FilePermissionsSelector.new(:contain => ['644', '755'])).watch do |filename, event|3FileWatcher.new( '.', 4 :filter => FilePermissionsSelector.new(:exclude => ['644', '755'])).watch do |filename, event|5FileWatcher.new( '.', 6 :filter => FilePermissionsSelector.new(:include => ['644', '755'])).watch do |filename, event|7FileWatcher.new( '.', 8 :filter => FilePermissionsSelector.new(:include => ['644', '755'])).watch do |filename, event|9FileWatcher.new( '.', 10 :filter => FilePermissionsSelector.new(:exclude => ['644', '755'])).watch do |filename, event|11FileWatcher.new( '.', 12 :filter => FilePermissionsSelector.new(:exclude => ['644', '755'])).watch do |filename, event|
contain
Using AI Code Generation
1FileUtils.touch("test.txt")2FileUtils.chmod(0600, "test.txt")3if f.contain?(0600, "test.txt")4FileUtils.chmod(0644, "test.txt")5if f.contain?(0644, "test.txt")6FileUtils.chmod(0777, "test.txt")7if f.contain?(0777, "test.txt")8FileUtils.chmod(0755, "test.txt")9if f.contain?(0755, "test.txt")10FileUtils.chmod(0666, "test.txt")11if f.contain?(0666, "test.txt")12FileUtils.chmod(0444, "test.txt")13if f.contain?(0444, "test.txt")14FileUtils.chmod(0222, "test.txt")15if f.contain?(0222, "test.txt")16FileUtils.chmod(0111, "test.txt")
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!