Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

10-25-2018 , 10:07 PM
If you think that's trolling I invite you to tell me how great windows batch scripting is (it isn't) or powershell (it also reallllly isn't)

Anyway a key to remember here is that your boolean part is really arguments to the "test" command, and it has to obey the syntax of commands. You don't expect "ls/bin" to work right? How come it insists on that space?

Anyway, the syntax you have here is "sh" syntax, which is quite old. You might prefer some of the improvements made in bash if syntax. It's not that different but there are fewer gotchas/surprises. Here's some info (http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-25-2018 , 10:08 PM
Quote:
Originally Posted by suzzer99
Thanks dude. A few general questions:
  1. What do you use for C# unit testing?
  2. Does using VS Code for C#/.net development make any sense - or does it have to be Visual Studio?
I don't have strong opinions on the others but for these two, definitely xUnit and actual Visual Studio. VS Code kind of sucks in comparison though it's gotten better.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-25-2018 , 10:46 PM
React Native with this Expo app is pretty darn cool
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 01:50 AM
I just spent 4 hours trying to figure out how to mock a method/class that some generic input/output types. OMG look how powerful - you can pass any type of object! Wow - I can't imagine a language where you could just do that out of the box.

And then you have to use reflection just to assert a property you want to test.

Me: It's a stupid mock test, I know it's an EmailInput object, just let me get the email property. I don't care if it breaks. Please? Can I cast it or something?

Compiler: Lol no. Go get your reflection shine box and start introspecting.

Nope. Don't miss types one bit.

Even C# is like yeah SomeAwesomeClass<GenericCrapT, MoreGenericCrapT> = new SomeAwesomeClass<GenericCrapT, MoreGenericCrapT>(); is kinda ridic so we'll just let you use var.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 07:00 AM
Quote:
Originally Posted by suzzer99
Ah the sheer joy of shell-scripting. TIL that this works:

Code:
if [ "$failed" = "0" ]; then
while this:

Code:
if ["$failed" = "0"]; then
Fails and says "command not found". I mean duh that's just common sense.

But here's the really fun part.This always evaluates to true no matter what values $failed has:

Code:
if [ "$failed"="0" ]; then
Apparently, much like the spaces inside the square brackets, those spaces around the equal sign are very important! Argh - that was a fun hour of my life. Like were the designers of Unix just trolling everybody with this ****?

The great part is if I actually figure out how to do what I'm trying to do, I have to replicate it all over again in windows. Time to abort.

For the last one you can use == for string comparisons, which is what you are doing. As written i think the interpreter thinks you might be doing some type of assignment without the space. If you want to compare the contents of failed to the val 0, use ${failed} -eq 0

If you need any help let me know, I’ve become pretty confident with my shell script abilities. It’s not something you should lightly jump into in my opinion, it’s super easy to write god awful scripts that just work anyway because bash is awesome.

Bash is a really beautiful and powerful language, there’s really only a few simple rules that are easy to learn. I get much more frustrated with system commands.

Also, fun trick - if you are using one single if, you can just ditch the if.

You can instead write something like:

[[ ${failed} -eq 0 ]] && do something

Second statement only gets evaluated if first statement is true. It’s less messy in my opinion then having stupid ifs/fi’s littered everywhere.

Last edited by jmakin; 10-26-2018 at 07:18 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 07:03 AM
One of my biggest bash pet peeves ever is people who pipe like 40 commands into each other with greps/awks/seds. There’s almost always an easier and way more readable way to do whatever goofy **** you’re trying to do. It’s completely unreadable to me.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 07:12 AM
I used to do error checking everywhere in my scripts with ifs/else's and checking $? a lot until my mentor taught me a much better way.

I write all my scripts like this now, at the top I use:

Code:
#! /bin/bash -e
-e is equivalent to "set -e" which exits the shell on any exit code that isn't 0. Then, I simply put a trap in the beginning of my script to catch exit, like this:

Code:
clean_exit () {
    ret_code=$? #return code that triggered trap
    *do some cleanup here*
    echo "Exit with status $ret_code"
}

trap clean_exit EXIT #calls my function on any exit

* code goes below here *

# some comparison I expect to fail sometimes:
set +e; [[ $some_var -eq 0 ]] && do something; set -e
Any failed command will go to your exit function this way so you don't really need to error check, you just need to remember to put set +e before any command you expect to fail sometimes and you are ok with.

For the scripts I write any command returning an error status is usually really bad so I prefer to bail ASAP. Maybe for your use case this isn't true.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:09 AM
You can do the if without the "if" in javascript too. Imo, though, basically more English words make things more readable.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:11 AM
Quote:
Originally Posted by ChrisV

3) Yeah the annotations (they're called attributes) are nice. They're not used for the default documentation, but they are commonly used for other things. For example, if you're writing a REST API you annotate methods with [HttpPost] or [HttpGet], which should be self explanatory.
ya these are great for writing APIs. you can also annotate the fields that the API receives or sends back. you can set the [ValidValues(")] or set them as [Required] or whatever.

Quote:
4) No. Opinions differ. I presume you know that var is just syntactic sugar - the variable is still typed, the compiler infers the type. Using actual untyped variables (with "dynamic") is frowned on.

Personally I avoid using var - the one time I use it is in ExtremelyLongTypeName x = new ExtremelyLongTypeName(); where if var is used you can still easily see what the type is reading the code. But Resharper (a VS addon which I recommend) by default has a style rule recommending the use of var, so like I said, opinions differ.
I was taught that var is the standard and proper way to initialize stuff in C#. string, or int or whatever is now a no-no.

Quote:
5) I haven't used VS Code yet so it's hard to say, but VS is pretty good.
havent used VS Code for C# but use it for Javascript and html stuff. Visual Studio is much heavier and has a ton more features so maybe that is why it is used for C# projects as they tend to be bigger and require more computing. I think its standard to use VS for c# stuff, esp APIs.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:20 AM
Thanks guys - good stuff on C#. I'll report back on how it goes.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:33 AM
Quote:
Originally Posted by suzzer99
I just spent 4 hours trying to figure out how to mock a method/class that some generic input/output types. OMG look how powerful - you can pass any type of object! Wow - I can't imagine a language where you could just do that out of the box.

And then you have to use reflection just to assert a property you want to test.

Me: It's a stupid mock test, I know it's an EmailInput object, just let me get the email property. I don't care if it breaks. Please? Can I cast it or something?

Compiler: Lol no. Go get your reflection shine box and start introspecting.
If you just want to test one property and that's all, it's appropriate to use type dynamic. If you declare a variable as "dynamic x" it will let you invoke any property or method you want on the object. If you're OK with it throwing a runtime exception if the invocation target doesn't exist, then go for it.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:35 AM
Ah - didn't know that. It will make life much easier for testing.

Also I haven't played around with Moq yet but I see it everywhere.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:47 AM
Quote:
Originally Posted by jmakin
[[ ${failed} -eq 0 ]] && do something
You can actually usually just do
$failed && something
because 0 = true in bash and any other number = false.

Quote:
Originally Posted by jmakin
I used to do error checking everywhere in my scripts with ifs/else's and checking $? a lot until my mentor taught me a much better way.
-e is a really good idea for most scripts and I'd actually usually prefer it was the default that you'd need to override. -x is a good one for debugging because it shows all the commands you're running and their output. I wish most scripting languages had this feature.

Something to keep in mind with -e is that a lot of programs are not very good about the "non-zero exit code means error", or, it may be ok for them to fail. You can handle these by running them like
(mycommand || true)
which runs them in a subshell and will always return a "true" result for error checking.

For example a lot of my build scripts start by cloning the a repo. If you run them locally, instead of on the build system, it's common for you to already have the repo checked out. Instead of checking for that first, or deleting the repo and re-downloading, I just let "git clone" silently fail.

And btw subshells are really your friend in bash, because anything you put in them acts like it was run in a separate script - they won't pollute the global namespace. Like...
Code:
(
    cd /foo
    git clone myrepo
    make
    make install
)
The parens make it run in a sub-shell so the change of directory is local to the part in parens and won't affect the top level script. This is especially good for stuff that might fail, because it won't leave you stranded in the wrong directory.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:49 AM
Quote:
Originally Posted by microbet
You can do the if without the "if" in javascript too. Imo, though, basically more English words make things more readable.
One of my favorite initialization techniques in languages that support it is like

Code:
foo = bar || "default"
which is cleaner and easier to read than

Code:
foo = "default"
if bar:
    foo = bar
or other variants that are essentially the same thing. This only works if when "bar" is in an unset state, it evaluates as false though. A lot of languages take this convention, in python, all the following evaluate to false:
None
[]
""
{}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 09:52 AM
We have a new hiring rule at my job. Basically, all programming applicants must have a CS degree (no math/physics/ee/etc) from a top-10 school or must be provably gods gift to programming. The majority of people working here now couldn't get hired under these criteria. I had forgotten about this until recently when I started going through resumes to bring people in to interview and my boss recommended me that some of the ones I'd shortlisted were not going to make it past the CTO.

It's a little weird to work someplace that wouldn't hire you.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 10:10 AM
Is the CTO much of a programmer? That requirement seems dumb. Maybe for people fresh out of college, but after a few years of professional experience, nah.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 11:05 AM
Quote:
Originally Posted by RustyBrooks
You can actually usually just do
$failed && something
because 0 = true in bash and any other number = false.



-e is a really good idea for most scripts and I'd actually usually prefer it was the default that you'd need to override. -x is a good one for debugging because it shows all the commands you're running and their output. I wish most scripting languages had this feature.

Something to keep in mind with -e is that a lot of programs are not very good about the "non-zero exit code means error", or, it may be ok for them to fail. You can handle these by running them like
(mycommand || true)
which runs them in a subshell and will always return a "true" result for error checking.

For example a lot of my build scripts start by cloning the a repo. If you run them locally, instead of on the build system, it's common for you to already have the repo checked out. Instead of checking for that first, or deleting the repo and re-downloading, I just let "git clone" silently fail.

And btw subshells are really your friend in bash, because anything you put in them acts like it was run in a separate script - they won't pollute the global namespace. Like...
Code:
(
    cd /foo
    git clone myrepo
    make
    make install
)
The parens make it run in a sub-shell so the change of directory is local to the part in parens and won't affect the top level script. This is especially good for stuff that might fail, because it won't leave you stranded in the wrong directory.
-u is another favorite of mine if you don’t want uninitialized variables wreaking havoc, aka the main cause of most evil shell script bugs. for instance:

Code:
rm -rf $misnamed_var/
woops byebye OS.

You can also do || true after a command you expect to fail but I don’t really do it because I think it’s less readable.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 11:20 AM
Ok everyone, now show me how to do all this stuff in the windows CMD shell.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 11:24 AM
HAHAHA

For real though gl with that. I hate windows shell so much.

You can run a linux shell in windows now but I have no idea how good/awful it is.

I looked at one if our scripts and all I saw was semicolons before every line and I noped the hell out of there.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 11:30 AM
The key here is it has to be simple and path of least resistance for devs to follow. Otherwise they will just give up and all do things their own way - which makes it impossible to standardize anything.

It's possible to make them install another program. But I'd rather avoid it. I seem to recall all kinds of hassle getting make or some make equivalent to run on windows.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 11:53 AM
Quote:
Originally Posted by suzzer99
Ok everyone, now show me how to do all this stuff in the windows CMD shell.
At least use Powershell. I have a lot of serious problems with it, but it is *miles* beyond what you can do in batch scripting or cmd shell for windows. Also it has access to most of the .NET api so if you do C# you can often figure out how to accomplish the same thing in powershell.

I just replaced a big shell script thing with powershell. Prior to that, I was doing part of a thing on windows, transferring some files to a linux box, and doing the rest there. Redoing in in powershell means I can do it all on windows and that means I can probably finish making it completely automated.

The equivalent in -e in powershell is a lot harder than in bash. It's this:
$ErrorActionPreference = "Stop"

But any command line thing you run might foul this up, so you typically need to run those in a particular way. I ganked a function from somewhere online that handles the details
Code:
function exec {
    param
    (
        [ScriptBlock] $ScriptBlock,
        [string] $StderrPrefix = "",
        [int[]] $AllowedExitCodes = @(0)
    )
 
    $backupErrorActionPreference = $script:ErrorActionPreference
 
    $script:ErrorActionPreference = "Continue"
    try
    {
        & $ScriptBlock 2>&1 | ForEach-Object -Process `
            {
                if ($_ -is [System.Management.Automation.ErrorRecord])
                {
                    "$StderrPrefix$_"
                }
                else
                {
                    "$_"
                }
            }
        if ($AllowedExitCodes -notcontains $LASTEXITCODE)
        {
            throw "Execution failed with exit code $LASTEXITCODE"
        }
    }
    finally
    {
        $script:ErrorActionPreference = $backupErrorActionPreference
    }
}
which you can call like

Code:
    exec { cd osquery; git checkout $OSQUERY_BRANCH; .\tools\make-win64-dev-env.bat; .\tools\make-win64-binaries.bat }
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 11:55 AM
Quote:
Originally Posted by microbet
Is the CTO much of a programmer? That requirement seems dumb. Maybe for people fresh out of college, but after a few years of professional experience, nah.
I think his background is in network security but I'm not sure. I think it's a reaction to the perception that we've hired some people in the past who aren't great, but the truth is
a) we have a lot of people who ARE great who would not be hired under this rubric
b) it probably won't really change things that much except that it'll drastically cut down our pool of potential applicants.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 11:58 AM
FFS time for my Friday rant:

If you can't ****ing write code that doesn't ****ing suck and then have someone do the code review and doesn't see that it ****ing sucks and merges it to master I'm going to ****ing lock down the repo and make it so only I can do merges. You have a "renderRow" function that maps through an array and outputs JSX? And it puts the same ****ing hardcoded ID on the container?? Gee I wonder if IDs should be unique or not? And then even worse you don't even ****ing use the ID anywhere in the entire repo other than where its created? What the **** are you doing? What the **** is happening in code review? For ****s sake.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 12:04 PM
Could be worse, we dont even really have code review
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
10-26-2018 , 12:24 PM
Man so we have all the compliance certificates now and it's made some things a pain in the ass. Everything has to be handled via MFA and we also use a password manager for almost everything so typically to log into any service I have to
1a) log into my password manager - involves hardware MFA
1b) log into our service manager - involves SMS MFA
2) log into the actual service, involves either SMS or hardware MFA depending

and then if I'm idle for an hour, do it all again. I usually only have to do 1 of 1a or 1b but twice in a few cases. The worst thing is I am usually following a bookmark so that's step 0) but it'll fail because I've been logged out. It is really annoying esp when an incident occurs and I need to open 5 different websites which is going to involve typing in at minimum 7 different random numbers.

Plus my google mfa auth thing now has 11 different token generators in it so I have to be sure I pick the right one, or it'll fail and I'll have to do it again.

God forbid I have to change a password - I use randomly generated ones and all these services have different rules for generating passwords so I usually have to tweak the generator. If I don't successfully save the new password in the password manager at the same time that I find one that works I might successfully change it but not have it saved and then I can't log it at all without involving IT. Most of these services require password changes between every 30 and 90 days. I change a password once a week.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m