Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Check for root under Cygwin

by vsespb (Hermit)
on Sep 02, 2013 at 09:57 UTC ( #1051914=perlquestion: print w/ replies, xml ) Need Help??
vsespb has asked for the wisdom of the Perl Monks concerning the following question:

I have unit tests, which check that my code properly handle "permission denied" errors and other IO errors.
My application works only under POSIX systems (i.e. not Win32), and I am thinking now about porting it to CygWin.
Tests look like this (simplified version):
use strict; use warnings; use Test::More tests => 2; my $file = 'somefile.tmp'; unlink $file; open my $f, '>', $file; close $f; chmod 0000, $file; SKIP: { skip "cannot run under root", 2 unless $<; ok ! eval { somefunc($file); 1; }; ok $@ =~ /cannot open file/; } unlink $file;
and somefunc() looks like this:
sub somefunc { open my $f, "<", shift or die 'cannot open file' };
(that is a simplified version too - just for proof of concept)
In the test you can see
skip "cannot run under root", 2 unless $<;
That's because otherwise test will fail under root.
That check, however, is not working under Cygwin as expected.
And I am wondering how that code could be fixed under cygwin?
(note that I am aware of several file-system emulator modules, but I decided to stick with real filesystem, for now)

Comment on Check for root under Cygwin
Select or Download Code
Re: Check for root under Cygwin
by Laurent_R (Vicar) on Sep 02, 2013 at 11:36 UTC

    What is the value of $< when you are logged on Cygwin?

      When I run cygwin session normal way, $< is 1001 and I am getting permission denied errors.

      If I start this session with "run as administrator", there are no more "permission denied" errors, but $< is same (1001)
Re: Check for root under Cygwin
by syphilis (Canon) on Sep 02, 2013 at 11:59 UTC
    That check, however, is not working under Cygwin as expected

    Root does not exist on Cygwin - so, something like:
    skip "cannot run under root", 2 if(!$< || $^O =~ /cygwin/i);
    Cheers,
    Rob
      Yes, skipping those tests would work for sure.
      But is there an option to run tests, when possible? (i.e. determine if current cygwin user acts like root under *nix)
        But is there an option to run tests, when possible?

        You mean that the tests *should* be run when running as administrator ? Perhaps Win32::IsAdminUser() is useful for determining that.
        See the Win32 module's documentation.

        Consider posting to the Cygwin mailing list (if you fail to receive a satisfactory answer here).

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1051914]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-07-31 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls