Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Convert to Lib tests #373

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

guygastineau
Copy link
Contributor

@guygastineau guygastineau commented Aug 6, 2019

This is not intended for merging at all, but rather it is intended to share ideas about how to convert our tests to library testing. That being said, if commits only ever involve 1 exercise, then we can easily cherry pick conversions where we agree, so they can be merged in their own PR.

For now, I just did two-fer initial PoC and triangle since it is a bit more relevant.

@glennj I played with the :: you used for name spacing in forth, but I could not get it to work for triangle. It kept returning an error status. Anyway, I will keep investigating that, but any investigation you feel like doing would be appreciated.

I noticed that nounset absolutely may not be used in the implementation files for lib testing. sourceing the file into bats, then pollutes the bats environment. Since bats relies on variables being unset sometimes it will not work. We can probably use something like bash -c "source triangle; equilateral 2 2 2" to keep from polluting the bats environment. We should make sure we discuss this ;)

closes #366


Reviewer Resources:

Track Policies

@guygastineau guygastineau changed the title WIP: Lib tests WIP: Convert to Lib tests Aug 6, 2019
@guygastineau
Copy link
Contributor Author

As I said over in #366 I wont force push to this, so feel free to clone and we can all work together on it.

I think local work on any given exercise should be rebased before pushing back up here to make cherry picking easier.

@kotp @glennj @IsaacG Let me know if you guys would prefer having this branch belong to our exercism repo outright. It might be nice if we could all just have our own version of this branch through our personal clones. Anyway, just let me know ;)

@guygastineau
Copy link
Contributor Author

guygastineau commented Aug 6, 2019

travis_fold:start:before_script
�[0Ktravis_time:start:1ae0802b
�[0K$ bin/fetch_configlet

gzip: stdin: unexpected end of file
tar: Unexpected EOF in archive
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
travis_time:end:1ae0802b:start=1565059734245936845,finish=1565060043499524003,duration=309253587158
�[0K�[31;1mThe command "bin/fetch_configlet" failed and exited with 2 during .�[0m

Your build has been stopped.

I have seen this happen before. Was it here? I think it is happening, because the files are .xz, but tar wants to treat them like gzip format due to an extension.

EDIT:

I remember now that I just needed to rerun the travis tests, because sometimes stuff fails for no reason 😟

echo "false"
fi

[[ $1 -eq 0 ]] && echo 'true' || echo 'false'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(( $1 == 0 )) && ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are very right about that.

At least I upgraded it from [] 😂

# Test returns true if the triangle is equilateral

@test "all sides are equal, equilateral" {
#[[ $BATS_RUN_SKIPPED == true ]] || skip
run bash triangle.sh equilateral 2 2 2
run equilateral 2 2 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a fan of exporting a single main or triangle function that takes a triangle-type as a first arg.
Thoughts?

Copy link
Contributor Author

@guygastineau guygastineau Aug 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that it is a library I think main here is not right, but I could be convinced that a managing function has its place for namespacing.

This also depends on how our other endeavors into namespacing fare.

If you have some ideas for this exercise please feel free to make a PR to this branch on my fork.

If you would prefer I am interested in making a new branch on the base repo for getting through this ordeal.

I want to know other contributor opinions first though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can make your own PR for triangle.

Copy link
Contributor

@glennj glennj Aug 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment thread is getting to the heart of how we want to manage name-spacing in our bash libs.

  1. do we want any? i.e. let source just dump library functionality into the "global" scope
  2. use a top-level function with library functions accessed as sub-commands, as suggested here
  3. use a function prefix, like I did in the forth example

Does anyone know of any common idioms or best practice guidelines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the world of bash library modules is all cowboys. We should see if Gnu org has any specification.

I learned that functions may have . in their names too.

The only problem I have is that my syntax highlighters get confused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I did some more tests.
Double colon (::) looks nice, but I have also read that it is not guaranteed to work with older versions of bash. I am of the opinion that there is absolutely no reason to be using bash older than v4.x, but historically we have not wanted to make Mac users upgrade bash against their will.

I am not against setting v4.2 as a requirement for the track. Hell, I am on v5.0.7, but I would not make this requirement all on my own.

If we decide against enforcing a minimum Bash version, then I see no other way to namespace the functions than by using an module access function.

In order for this not to confuse students it should be present in the stub. We would need to decide if we write the whole thing, so they don't have to worry about it or not.

Then we are left with the decision about how to name the parts it calls. Good naming convention would then make the former API functions internal and they should be preceded by a _. I think this will get ugly very quickly, and it is not really exposing students to a common pattern in the shell world. Furthermore, it doesn't do much to help with possible collisions in the world.

While I think that :: is ideal (even if a bit non-standard) I am feeling less comfortable with the interface function pattern. I honestly think that normally lib sourcing is the way to go here.

If it makes us feel any better. Golang's tests are in the same packagespace as the code, so all of the functions are called without namespacing.

Also, our namespacing ideas were never going to solve issue of set options colliisions with bats' internal requirements. To do that we would need to execute the run commands in a sub-shell. This might work, but it needs to be tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@issacg I guess we were typing at the same time 🤣

It seems we both came to the same conclusion (although I am very verbose as usual).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking around at other languages like Python and Ruby it is very evident tha no one else is worrying about this (but in their languages those functions will be namespaced once they are imported somewhere). That just isn't the way of life here 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note bash 3 (or, at least 3.2.57) supports space::func just fine. While we may not want to enforce bash 4+, I think it's fair to expect bash 3 or newer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking.

@guygastineau
Copy link
Contributor Author

guygastineau commented Aug 6, 2019

Additionally, if these are bash libraries (they are not posix compliant) then they should have .bash as their suffix.

@kotp
Copy link
Member

kotp commented Aug 6, 2019

The invalid and wontfix labels are added just to remind not to merge.

@guygastineau
Copy link
Contributor Author

Thank @kotp those labels are a good reminder.

Do you have an opinion on whether or not this project should be switched to an architecture wherein it is a direct branch of exercism/bash?

@kotp
Copy link
Member

kotp commented Aug 6, 2019

I think it is fine where it is, until it isn't. We can clone it down and work with it locally, as it is.

@glennj
Copy link
Contributor

glennj commented Aug 14, 2019

FYI I have not noticed any problems with function names containing :: in bash 3.2 on the mac

@guygastineau
Copy link
Contributor Author

So, is it possible to get new count of our positions regarding namespaces?
The only thing that still concerns me about using :: is that it bars the solution from being posix compatible if a student wishes to do so, but that is not a big deal in my opinion.

Still my vote would be for no namespaces, since students are unlikely to encounter that so much in wild (although it does exist).

@guygastineau
Copy link
Contributor Author

guygastineau commented Aug 14, 2019

Tally for namespacing:

Contributors With Function With :: None
@guygastineau ✔️
@glennj ✔️
@IsaacG ✔️
@kotp

@glennj
Copy link
Contributor

glennj commented Aug 14, 2019

So, is it possible to get new count of our positions regarding namespaces?
The only thing that still concerns me about using :: is that it bars the solution from being posix compatible if a student wishes to do so, but that is not a big deal in my opinion.

Since this is a bash-specific track and bash is not POSIX compatible, IMO don't care.

Still my vote would be for no namespaces, since students are unlikely to encounter that so much in wild (although it does exist).

This is like import library vs from library import * in python. My vote is yes, but I'll abide by majority.

@guygastineau what's "with Function" mean? Is that capitalizing the library function names?

@IsaacG
Copy link
Member

IsaacG commented Aug 14, 2019

I'll vote for yes namespace.

@guygastineau
Copy link
Contributor Author

@IsaacG I assume you mean with :: from our earlier conversations?

@guygastineau
Copy link
Contributor Author

@glennj I meant using a function that takes subcommands to act as the entry point for the library.

@IsaacG
Copy link
Member

IsaacG commented Aug 14, 2019

Yes, I'm in favor of using the :: for function namespaces.

@glennj
Copy link
Contributor

glennj commented Aug 14, 2019

ref #376

@kotp
Copy link
Member

kotp commented Aug 15, 2019

I feel like this is the Bash track, and so by definition not sh and therefore not required to be POSIX compliant. sh is a specification, and Bash is a superset of sh. I do agree that we should be doing the .bash extension, instead. And there should be a clear explanation for that in the introductory material handed to students.

It may be interesting to have a sh track as well, where strict compliance is expected, but that isn't this track. In recent times, I have noticed:

file $(which sh)
/bin/sh: symbolic link to dash

This seems to happen on Debian (and at least some derivatives). OpenBSD seems to use something a little more Korn-y.

So the practice of "portability" using !#/bin/sh is not an issue here, but could also be mentioned.

Instead, as this is the Bash track, we should use #!/usr/bin/env bash or at least specifically #!/bin/bash and let the program itself fail if Bash isn't available. Again, the focus being "This is the Bash track".

Just some thoughts as it regards my "vote" in Tally for Namespacing.

@glennj
Copy link
Contributor

glennj commented Aug 16, 2019

... I do agree that we should be doing the .bash extension, instead.

How is that going to affect the CI scripts? I guess we'll have to throw a little extra logic into bin/validate_exercises

@kotp
Copy link
Member

kotp commented Aug 16, 2019

... I do agree that we should be doing the .bash extension, instead.

How is that going to affect the CI scripts? I guess we'll have to throw a little extra logic into bin/validate_exercises

No direct answer to this, but no (long term) logic. All should be .bash in my opinion.

Base automatically changed from master to main January 28, 2021 19:16
@MichaelDimmitt
Copy link

Really good discussion here 🔥

another thing worth noting,
diffing is easy to attain in bats

by changing

@test "a name given" {
  [[ $BATS_RUN_SKIPPED == true  ]] || skip
  run two_fer Alice
  [[ $status -eq 0 ]]
  [[ $output == "One for Alice, one for me." ]]
}
printDiff(){
  echo output;
  echo "$output";
  echo ;
  echo expected;
  echo "$expected;
}

@test "a name given" {
  [[ $BATS_RUN_SKIPPED == true  ]] || skip
  run two_fer Alice
  expected=$(cat <<-END
One for Alice, one for me.
END
  )
  printDiff
  [[ $status -eq 0 ]]
  [[ "$output" ==  "$expected" ]]
}

@ErikSchierboom
Copy link
Member

And as a side note: the test runner should also be updated. It might even be necessary to support both formats to not break old exercises. Did we decide how to handle such a situation @iHiD ?

@kotp kotp marked this pull request as draft December 7, 2021 20:39
@IsaacG
Copy link
Member

IsaacG commented Mar 23, 2022

This PR has seen no activity in 6 months. Is it still being used or should it be closed out?

@kotp
Copy link
Member

kotp commented Mar 23, 2022

Leave it be. It is draft and still has a place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch Tests to Library Tests
6 participants