Published on :

Git Add Files Test (mp_gitadd)

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script initializes a working directory, then clones the `https://github.com/sasjs/core` repository into it. It then performs a series of operations on files: creating a new file (`somefile.txt`), modifying the content of an existing file (`readme.md`), and deleting another (`package.json`). After these changes, it uses the `mp_gitstatus` macro to check that three files are 'unstaged'. It then uses the `mp_gitadd` macro to stage these changes, and finally, it rechecks the Git status to ensure that the three files are now 'staged', using assertions (`mp_assert`) to validate each step of the process.
Data Analysis

Type : MIXED


The script interacts with an external source by cloning the Git repository `https://github.com/sasjs/core`. It also creates temporary internal datasets (`work.gitstatus`, `work.gitstatus2`) to store the results of Git commands and `work.test_results` for unit test assertions.

1 Code Block
MACRO CALL / SYSCALL
Explanation :
This block initializes the macro variable `dir` with the path to a 'core' subdirectory created in the SAS working directory. It then uses the `GITFN_CLONE` function to clone the Git repository `https://github.com/sasjs/core` into this directory specified by `&dir`. This operation is a direct interaction with the file system and an external Git service.
Copied!
1%let dir = %sysfunc(pathname(work))/core;
2%put
3SOURCE clone rc=%sysfunc(GITFN_CLONE(https://github.com/sasjs/core,&dir));
4 
2 Code Block
MACRO CALL Data
Explanation :
These SASJS macro calls simulate modifications in the cloned repository: `mf_writefile` is used to create a new file (`somefile.txt`) with content and to modify the content of an existing file (`readme.md`). The `mf_deletefile` macro deletes an existing file (`package.json`). These actions are direct file system manipulations to prepare changes for testing with Git commands.
Copied!
1%mf_writefile(&dir/somefile.txt,l1=some content)
2%mf_writefile(&dir/readme.md,l1=new readme)
3%mf_deletefile(&dir/package.json)
3 Code Block
MACRO CALL / PROC SQL Data
Explanation :
This block uses the `mp_gitstatus` macro to get the current status of files in the Git repository (`&dir`) and stores the results in the temporary dataset `work.gitstatus`. A `PROC SQL` then counts the number of unstaged files (where the 'staged' column is 'FALSE') and stores the result in the macro variable `test1`. Finally, the `mp_assert` macro is used to verify that this number is indeed 3, confirming that the previous modifications are correctly detected as unstaged by Git.
Copied!
1%mp_gitstatus(&dir,outds=work.gitstatus)
2 
3%let test1=0;
4PROC SQL noprint;
5select count(*) into: test1 from work.gitstatus where staged='FALSE';
6 
7%mp_assert(
8 iftrue=(&test1=3),
9 desc=3 changes are ready to add,
10 outds=work.test_results
11)
4 Code Block
MACRO CALL
Explanation :
The `mp_gitadd` macro is called to add the modified files (identified by the `work.gitstatus` dataset) to the Git staging area in the repository specified by `&dir`. The parameter `mdebug=&sasjs_mdebug` potentially activates debug mode for the macro.
Copied!
1%mp_gitadd(&dir,inds=work.gitstatus,mdebug=&sasjs_mdebug)
5 Code Block
MACRO CALL / PROC SQL Data
Explanation :
After the add operation, this block calls `mp_gitstatus` again to get the new status of files in the repository (`&dir`), storing the results in the temporary dataset `work.gitstatus2`. A second `PROC SQL` counts the files where the 'staged' column is 'TRUE' (i.e., staged files) and stores the result in `test2`. The `mp_assert` macro is used to confirm that 3 files are now 'staged', thus validating the success of the `mp_gitadd` operation.
Copied!
1%mp_gitstatus(&dir,outds=work.gitstatus2)
2%let test2=0;
3PROC SQL noprint;
4select count(*) into: test2 from work.gitstatus2 where staged='TRUE';
5 
6%mp_assert(
7 iftrue=(&test2=3),
8 desc=3 changes were added,
9 outds=work.test_results
10)
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.