Skip to content

Commit

Permalink
Improved Testing workflow and quality (#24)
Browse files Browse the repository at this point in the history
* Added basic testcase flow

* Updated the testing workflow

* Fix name of workflow

* Added source code for testing and fixed path

* add test source
  • Loading branch information
shivasurya authored May 10, 2024
1 parent b44f8b2 commit 27c2484
Show file tree
Hide file tree
Showing 17 changed files with 489 additions and 45 deletions.
16 changes: 11 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
name: Build and Release
name: Build and Test

on:
push:
branches:
- main
- master
pull_request:
types: [closed]
branches: [main]

jobs:
build:
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true
steps:
- name: Set up Go
uses: actions/setup-go@v4
Expand All @@ -26,4 +27,9 @@ jobs:
- name: Build
run: |
cd sourcecode-parser
go build -o pathfinder
go build -o pathfinder
- name: Test
run: |
cd sourcecode-parser
go test -v ./...
90 changes: 50 additions & 40 deletions sourcecode-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,76 @@ import (
"strings"
)

func processQuery(input string, graph *CodeGraph, output string) {
func processQuery(input string, graph *CodeGraph, output string) (string, error) {
fmt.Println("Executing query: " + input)
lex := queryparser.NewLexer(input)
pars := queryparser.NewParser(lex)
query := pars.ParseQuery()
if query == nil {
fmt.Println("Failed to parse query:")
for _, err := range pars.Errors() {
fmt.Println(err)
}
} else {
entities := QueryEntities(graph, query)
if output == "json" {
// convert struct to query_results
queryResults, err := json.Marshal(entities)
if err != nil {
fmt.Println("Error processing query results")
} else {
fmt.Println(string(queryResults))
}
} else {
fmt.Println("------Query Results------")
for _, entity := range entities {
fmt.Println(entity.CodeSnippet)
}
fmt.Println("-------------------")
return "", fmt.Errorf("failed to parse query: %v", pars.Errors())
}
entities := QueryEntities(graph, query)
if output == "json" {
// convert struct to query_results
queryResults, err := json.Marshal(entities)
if err != nil {
return "", fmt.Errorf("error processing query results: %w", err)
}
return string(queryResults), nil
}
var result strings.Builder
result.WriteString("------Query Results------\n")
for _, entity := range entities {
result.WriteString(entity.CodeSnippet + "\n")
}
result.WriteString("-------------------\n")
return result.String(), nil
}

func main() {
// accept command line param optional path to source code
func executeProject(project, query, output string, stdin bool) (string, error) {
graph := NewCodeGraph()
output := flag.String("output", "", "Supported output format: json")
query := flag.String("query", "", "Query to execute")
project := flag.String("project", "", "Project to analyze")
flag.Parse()
// loop to accept queries
if project != nil {
graph = Initialize(*project)
if project != "" {
graph = Initialize(project)
}
// check if output and query are provided
if output != nil && query != nil && *output != "" && *query != "" {
processQuery(*query, graph, *output)
} else {

if stdin {
// read from stdin
for {
var input string
fmt.Print("Path-Finder Query Console: \n>")
in := bufio.NewReader(os.Stdin)

input, err := in.ReadString('\n')
if err != nil {
fmt.Println("Error reading input")
return
return "", fmt.Errorf("error processing query: %w", err)
}
// if input starts with :quit string
if strings.HasPrefix(input, ":quit") {
return
return "Okay, Bye!", nil
}
fmt.Print("Executing query: " + input)
processQuery(input, graph, "text")
result, err := processQuery(input, graph, output)
if err != nil {
return "", fmt.Errorf("error processing query: %w", err)
}
fmt.Println(result)
}
} else if output != "" && query != "" {
return processQuery(query, graph, output)
}
return "", fmt.Errorf("output and query parameters are required")
}

func main() {
// accept command line param optional path to source code
output := flag.String("output", "", "Supported output format: json")
query := flag.String("query", "", "Query to execute")
project := flag.String("project", "", "Project to analyze")
stdin := flag.Bool("stdin", false, "Read query from stdin")
flag.Parse()

result, err := executeProject(*project, *query, *output, *stdin)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(result)
}
40 changes: 40 additions & 0 deletions sourcecode-parser/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"testing"
)

func TestProcessQuery(t *testing.T) {
graph := NewCodeGraph()
output := "json"
input := "FIND variable_declaration WHERE visibility = 'private'"

result, err := processQuery(input, graph, output)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// Here you can add assertions based on what you expect the result to be.
// This will depend on the specifics of your processQuery function.
if result == "" {
t.Errorf("Expected result to be non-empty")
}
}

func TestExecuteProject(t *testing.T) {
// get project from command line
project := "../test-src/"
query := "FIND variable_declaration WHERE visibility = 'private'"
output := "json"

result, err := executeProject(project, query, output, false)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// Here you can add assertions based on what you expect the result to be.
// This will depend on the specifics of your executeProject function.
if result == "" {
t.Errorf("Expected result to be non-empty")
}
}
8 changes: 8 additions & 0 deletions test-src/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
4 changes: 4 additions & 0 deletions test-src/android/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# android Udacity
My leanings with Android Udacity
##Project 2 : The Movie App
Add the ACCESS TOKEN of the MovieDBAPI to FILE : <b>com.ivb.udacity.constants.constant.java</b> LINE NUMBER : 7 - ACCESS_TOKEN variable
1 change: 1 addition & 0 deletions test-src/android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
32 changes: 32 additions & 0 deletions test-src/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '23.0.2'

defaultConfig {
applicationId "com.ivb.udacity"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile 'com.android.support:cardview-v7:23.0.1'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.android.support:support-v4:23.0.1'
}
17 changes: 17 additions & 0 deletions test-src/android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in F:\Android\SDK/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ivb.udacity;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ivb.udacity;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
23 changes: 23 additions & 0 deletions test-src/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0-alpha3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
18 changes: 18 additions & 0 deletions test-src/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Binary file not shown.
6 changes: 6 additions & 0 deletions test-src/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Wed Oct 21 11:34:03 PDT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
Loading

0 comments on commit 27c2484

Please sign in to comment.