Skip to content

Commit f85a4f1

Browse files
committed
Add GolangXrefTest with one non-passing for dangling span
1 parent 17fd030 commit f85a4f1

File tree

7 files changed

+484
-0
lines changed

7 files changed

+484
-0
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
109109
<exclude>*.java</exclude>
110110
</excludes>
111111
</testResource>
112+
<testResource>
113+
<targetPath>org/opensolaris/opengrok/analysis/golang/</targetPath>
114+
<directory>../test/org/opensolaris/opengrok/analysis/golang/</directory>
115+
<excludes>
116+
<exclude>*.java</exclude>
117+
</excludes>
118+
</testResource>
112119
<testResource>
113120
<targetPath>org/opensolaris/opengrok/analysis/perl/</targetPath>
114121
<directory>../test/org/opensolaris/opengrok/analysis/perl/</directory>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opensolaris.opengrok.analysis.golang;
26+
27+
import java.io.BufferedReader;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.io.InputStreamReader;
32+
import java.io.OutputStream;
33+
import java.io.PrintStream;
34+
import java.io.StringWriter;
35+
import java.io.Writer;
36+
37+
import org.opensolaris.opengrok.analysis.CtagsReader;
38+
import org.opensolaris.opengrok.analysis.Definitions;
39+
import org.opensolaris.opengrok.analysis.FileAnalyzer;
40+
import org.opensolaris.opengrok.analysis.WriteXrefArgs;
41+
import org.junit.Test;
42+
import static org.junit.Assert.assertNotNull;
43+
import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual;
44+
45+
/**
46+
* Tests the {@link GolangXref} class.
47+
*/
48+
public class GolangXrefTest {
49+
50+
@Test
51+
public void sampleTest() throws IOException {
52+
writeAndCompare("org/opensolaris/opengrok/analysis/golang/sample.go",
53+
"org/opensolaris/opengrok/analysis/golang/sample_xref.html",
54+
getTagsDefinitions());
55+
}
56+
57+
@Test
58+
public void shouldCloseTruncatedStringSpan() throws IOException {
59+
writeAndCompare("org/opensolaris/opengrok/analysis/golang/truncated.go",
60+
"org/opensolaris/opengrok/analysis/golang/truncated_xref.html",
61+
null);
62+
}
63+
64+
private void writeAndCompare(String sourceResource, String resultResource,
65+
Definitions defs)
66+
throws IOException {
67+
68+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
69+
ByteArrayOutputStream baosExp = new ByteArrayOutputStream();
70+
71+
InputStream res = getClass().getClassLoader().getResourceAsStream(
72+
sourceResource);
73+
assertNotNull(sourceResource + " should get-as-stream", res);
74+
writeGolangXref(new PrintStream(baos), res, defs);
75+
res.close();
76+
77+
InputStream exp = getClass().getClassLoader().getResourceAsStream(
78+
resultResource);
79+
assertNotNull(resultResource + " should get-as-stream", exp);
80+
copyStream(exp, baosExp);
81+
exp.close();
82+
baosExp.close();
83+
baos.close();
84+
85+
String ostr = new String(baos.toByteArray(), "UTF-8");
86+
String gotten[] = ostr.split("\n");
87+
88+
String estr = new String(baosExp.toByteArray(), "UTF-8");
89+
String expected[] = estr.split("\n");
90+
91+
assertLinesEqual("Golang xref", expected, gotten);
92+
}
93+
94+
private void writeGolangXref(PrintStream oss, InputStream iss,
95+
Definitions defs)
96+
throws IOException {
97+
98+
oss.print(getHtmlBegin());
99+
100+
Writer sw = new StringWriter();
101+
GolangAnalyzerFactory fac = new GolangAnalyzerFactory();
102+
FileAnalyzer analyzer = fac.getAnalyzer();
103+
analyzer.setScopesEnabled(true);
104+
analyzer.setFoldingEnabled(true);
105+
WriteXrefArgs wargs = new WriteXrefArgs(
106+
new InputStreamReader(iss, "UTF-8"), sw);
107+
wargs.setDefs(defs);
108+
analyzer.writeXref(wargs);
109+
oss.print(sw.toString());
110+
111+
oss.print(getHtmlEnd());
112+
}
113+
114+
private void copyStream(InputStream iss, OutputStream oss)
115+
throws IOException {
116+
117+
byte buffer[] = new byte[8192];
118+
int read;
119+
do {
120+
read = iss.read(buffer, 0, buffer.length);
121+
if (read > 0) {
122+
oss.write(buffer, 0, read);
123+
}
124+
} while (read >= 0);
125+
}
126+
127+
private Definitions getTagsDefinitions() throws IOException {
128+
InputStream res = getClass().getClassLoader().getResourceAsStream(
129+
"org/opensolaris/opengrok/analysis/golang/sampletags");
130+
assertNotNull("though sampletags should stream,", res);
131+
132+
BufferedReader in = new BufferedReader(new InputStreamReader(
133+
res, "UTF-8"));
134+
135+
CtagsReader rdr = new CtagsReader();
136+
String line;
137+
while ((line = in.readLine()) != null) {
138+
rdr.readLine(line);
139+
}
140+
return rdr.getDefinitions();
141+
}
142+
143+
private static String getHtmlBegin() {
144+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
145+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" +
146+
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
147+
"<html xmlns=\"http://www.w3.org/1999/xhtml\"" +
148+
" xml:lang=\"en\" lang=\"en\"\n" +
149+
" class=\"xref\">\n" +
150+
"<head>\n" +
151+
"<title>sampleFile - OpenGrok cross reference" +
152+
" for /sampleFile</title></head><body>\n";
153+
}
154+
155+
private static String getHtmlEnd() {
156+
return "</body>\n" +
157+
"</html>\n";
158+
}
159+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2016 Junegunn Choi
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
// THE SOFTWARE.
22+
23+
package fzf
24+
25+
import (
26+
"bufio"
27+
"io"
28+
"os"
29+
"sync/atomic"
30+
"time"
31+
32+
"github.com/junegunn/fzf/src/util"
33+
)
34+
35+
// Reader reads from command or standard input
36+
type Reader struct {
37+
pusher func([]byte) bool
38+
eventBox *util.EventBox
39+
delimNil bool
40+
event int32
41+
}
42+
43+
// NewReader returns new Reader object
44+
func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool) *Reader {
45+
return &Reader{pusher, eventBox, delimNil, int32(EvtReady)}
46+
}
47+
48+
func (r *Reader) startEventPoller() {
49+
go func() {
50+
ptr := &r.event
51+
pollInterval := readerPollIntervalMin
52+
for {
53+
if atomic.CompareAndSwapInt32(ptr, int32(EvtReadNew), int32(EvtReady)) {
54+
r.eventBox.Set(EvtReadNew, true)
55+
pollInterval = readerPollIntervalMin
56+
} else if atomic.LoadInt32(ptr) == int32(EvtReadFin) {
57+
return
58+
} else {
59+
pollInterval += readerPollIntervalStep
60+
if pollInterval > readerPollIntervalMax {
61+
pollInterval = readerPollIntervalMax
62+
}
63+
}
64+
time.Sleep(pollInterval)
65+
}
66+
}()
67+
}
68+
69+
func (r *Reader) fin(success bool) {
70+
atomic.StoreInt32(&r.event, int32(EvtReadFin))
71+
r.eventBox.Set(EvtReadFin, success)
72+
}
73+
74+
// ReadSource reads data from the default command or from standard input
75+
func (r *Reader) ReadSource() {
76+
r.startEventPoller()
77+
var success bool
78+
if util.IsTty() {
79+
cmd := os.Getenv("FZF_DEFAULT_COMMAND")
80+
if len(cmd) == 0 {
81+
// The default command for *nix requires bash
82+
success = r.readFromCommand("bash", defaultCommand)
83+
} else {
84+
success = r.readFromCommand("sh", cmd)
85+
}
86+
} else {
87+
success = r.readFromStdin()
88+
}
89+
r.fin(success)
90+
}
91+
92+
func (r *Reader) feed(src io.Reader) {
93+
delim := byte('\n')
94+
if r.delimNil {
95+
delim = '\000'
96+
}
97+
reader := bufio.NewReaderSize(src, readerBufferSize)
98+
for {
99+
// ReadBytes returns err != nil if and only if the returned data does not
100+
// end in delim.
101+
bytea, err := reader.ReadBytes(delim)
102+
byteaLen := len(bytea)
103+
if byteaLen > 0 || byteaLen > 0xFF {
104+
if err == nil {
105+
// get rid of carriage return if under Windows:
106+
if util.IsWindows() && byteaLen >= 2 && bytea[byteaLen-2] == byte('\r') {
107+
bytea = bytea[:byteaLen-2]
108+
} else {
109+
bytea = bytea[:byteaLen-1]
110+
}
111+
}
112+
if r.pusher(bytea) {
113+
atomic.StoreInt32(&r.event, int32(EvtReadNew))
114+
}
115+
}
116+
if err != nil {
117+
break
118+
}
119+
}
120+
}
121+
122+
func (r *Reader) readFromStdin() bool {
123+
r.feed(os.Stdin)
124+
return true
125+
}
126+
127+
func (r *Reader) readFromCommand(shell string, cmd string) bool {
128+
listCommand := util.ExecCommandWith(shell, cmd)
129+
out, err := listCommand.StdoutPipe()
130+
if err != nil {
131+
return false
132+
}
133+
err = listCommand.Start()
134+
if err != nil {
135+
return false
136+
}
137+
r.feed(out)
138+
return listCommand.Wait() == nil
139+
}

0 commit comments

Comments
 (0)